有没有办法执行像元素一样的bsxfun
操作,但只能操作稀疏矩阵的非零元素?
特别是,对于A
位置(i,j)
I 中矩阵中的每个非零元素,我希望找到i
第 - 行中除了元素之外的所有非零元素的乘积(i,j)
.
例如,如果i-th
行如下所示:
0 5 3 0 0 4 0 0
Run Code Online (Sandbox Code Playgroud)
结果应如下所示:
0 12 20 0 0 15 0 0
Run Code Online (Sandbox Code Playgroud)
最明显的解决方案似乎是沿每行采用非零元素的乘积,然后将每个元素从行产品中分离出来.因此,在上面的例子中,行产品是5 x 3 x 4 = 60
,然后我只是分开5
3
并4
在各自的位置.
给定一个稀疏矩阵A
,这是我迄今为止最好的解决方案:
[M N] = size(A);
[row col vals] = find(A);
row_prod = accumarray(row,vals,[],@prod);
B = bsxfun(@ldivide, A, row_prod);
B = sparse(row,col,B(sub2ind([M N],row,col)),M,N);
Run Code Online (Sandbox Code Playgroud)
前三行实现了我想要的:一个列向量,表示每行非零元素的乘积.但是,最后两行存在很多问题.
bsxfun
将返回一个非稀疏矩阵的大小 A
Inf
或者-Inf
我真的只想要零.Inf
s,因为Matlab定义了零无穷大NaN
.我只需要咬紧牙关并为此写一个for循环吗?还是有另一种方法来接近它吗?
我想我已经找到了解决我上述大部分担忧的解决方案。有时,当我bsxfun
手里拿着锤子时,整个世界开始看起来像钉子。我忘记了我所做的一些简单乘法bsxfun
可以使用矩阵乘法同样轻松(并且可以说更易读)解决。虽然我不认为我对这个问题的解决方案更具可读性,但它比我上一个解决方案的效率高了几个数量级。
% 'This happens once, outside the loop, since the size'
% 'and sparsity structure of A dont change in the loop'
[M N] = size(A);
[row col] = find(A);
%% 'Inside iterative loop'
% 'Get the product of non-zero row elements'
row_prod = accumarray(row,nonzeros(A),[],@prod);
% 'Use row products to compute 'leave-one-out' row products'
B = spdiags(row_prod,0,M,M)*spfun(@(x) 1./x, A);
Run Code Online (Sandbox Code Playgroud)
如果可以改进的话,我仍然有兴趣听到其他建议。