Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Passing back data with increasing row indices using Sparse Command

조회 수: 1 (최근 30일)
Huck Febbo
Huck Febbo 2016년 3월 17일
마감: MATLAB Answer Bot 2021년 8월 20일
By default the sparse command passes back the non-zero elements in a matrix arranged by increasing column indices:
A = [ 0 0 1; 0 0 2; 0 2 1];
B = sparse(A)
B =
(3,2) 2
(1,3) 1
(2,3) 2
(3,3) 1
Is there a way to change this so that it passes back the sparse matrix in increasing row indices? In this case
B =
(1,3) 1
(2,3) 2
(3,2) 2
(3,3) 1

답변 (1개)

Walter Roberson
Walter Roberson 2016년 3월 17일
You are confusing the display of B with the content of B. The disp() and display() of sparse matrices is in the order you indicate. However that does not mean that the values are "passed back" in that order, just that they are displayed in that order. (Internally, they are in fact ordered by column, but that is an internal detail.)
You can do this:
[r, c, s] = find(B);
rcs = sort([r,c,s], 2);
fprintf('(%d,%d) %g\n', rcs.'); %the transpose of values is not obvious but it is consistent with the rest of MATLAB

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by