从列的非零条目中删除列均值
Remove column mean from nonzero entries of a column
给定 MATLAB 中的稀疏矩阵 A 及其列 m 中非零元素的平均值,是否可以减去每列中的非零元素来自每列的平均值并避免循环遍历列?
我正在寻找有效的解决方案。如果可以的话,使用 'bsxfun' 可能是一种解决方案。
谢谢
您可以使用 find
to get the column indices; use those to index into m
to do the subtraction; and put the results back into A
using logical indexing 的第二个输出:
A = sparse([0 0 0 0; 1 0 3 2; 2 1 0 5]); %// example data
m = [1.5 1 3 3.5]; %// vector of mean of nonzero elements of each column
m = m(:);
[~, jj, vv] = find(A);
A(logical(A)) = vv - m(jj);
原文A
:
>> full(A)
ans =
0 0 0 0
1 0 3 2
2 1 0 5
决赛 A
:
>> full(A)
ans =
0 0 0 0
-0.5000 0 0 -1.5000
0.5000 0 0 1.5000
给定 MATLAB 中的稀疏矩阵 A 及其列 m 中非零元素的平均值,是否可以减去每列中的非零元素来自每列的平均值并避免循环遍历列?
我正在寻找有效的解决方案。如果可以的话,使用 'bsxfun' 可能是一种解决方案。
谢谢
您可以使用 find
to get the column indices; use those to index into m
to do the subtraction; and put the results back into A
using logical indexing 的第二个输出:
A = sparse([0 0 0 0; 1 0 3 2; 2 1 0 5]); %// example data
m = [1.5 1 3 3.5]; %// vector of mean of nonzero elements of each column
m = m(:);
[~, jj, vv] = find(A);
A(logical(A)) = vv - m(jj);
原文A
:
>> full(A)
ans =
0 0 0 0
1 0 3 2
2 1 0 5
决赛 A
:
>> full(A)
ans =
0 0 0 0
-0.5000 0 0 -1.5000
0.5000 0 0 1.5000