Reputation: 1049
I am trying to find all non-zeros elements in a large sparse matrix K(19683000x19683000). I used find to first return the linear indices.
val = K(K~=0);
inds = find(K~=0);
[j, i] = ind2sub(size(K), inds);
% [j, i] = find(K~=0);
i = full(i);
j = full(j);
This gave some error:
Error using find
Matrix is too large to return linear indices.
Use [i,j] = find(S) for sparse matrix.
Error in (line 82)
inds = find(K~=0);
Error in run (line 64)
evalin('caller', [script ';']);
Any idea what is happening and how I can avoid it?
Upvotes: 3
Views: 185
Reputation: 112699
I am not familiar with that error (probably because I never used matrices so large). The error is likely related with the fact that double
variables can exactly represent integers up to 2^53
only. The number of elements of your matrix does not exceed that limit, but it is near.
Maybe you can sidestep the issue, up to 2^53
, by getting the row and column indices and then computing the linear index from those manually:
[ii,jj] = find(K);
inds = ii + (jj-1)*size(K,1);
If needed, you can push the limit up to 2^64
by using a uint64
linear index instead of double
:
[ii,jj] = find(K);
inds = uint64(ii) + (uint64(jj)-1)*size(K,1);
Upvotes: 3