3
感觉应该有一个简单的解决方案,但我不能找到它访问在稀疏矩阵的多个元素:MATLAB使用行和列索引矢量
我有稀疏矩阵A
B
具有相同尺寸n*n
。我想创建矩阵C
,它复制A
中的值,其中B
非零。
这是我的方法:
[r,c,v] = find(B);
% now I'd like to create an array of values using indices r and c,
% but this doesn't work (wrong syntax)
v2 = A(r,c);
% This won't work either
idx = find(B); % linear indexing, too high-dimensional
v2 = A(idx);
% and create C
C = sparse(r,c,v2,n,n);
这里有一些更多的细节:
- 我的矩阵是非常大的,所以解决方案必须有效。不幸的是,
C(B~=0) = B(B~=0);
不会这样做。 - 由于矩阵太大,线性索引不起作用(
Matrix is too large to return linear indices.
)。
是否真的没有办法使用二维索引?
感谢您的帮助!
你可以试试'C = A。*(B〜= 0);'? –
是的,非常感谢,这工作!如果你想发布它作为答案,我可以正式接受它:) – Lisa
当然。在您对数据进行尝试之前,我并不完全确定,因为我们正在谈论效率。感谢信用。 –