Given a matrix, find the locations and the values of all nonzero elements. Hence given the matrix
\[ \left ( {\begin {array}{ccc} 0 & 0 & 1 \\ 10 & 0 & 2 \\ 3 & 0 & 0 \end {array}} \right )\]
the positions returned will be \((1,3),(2,1),(2,3),(3,1)\) and the corresponding values are \(1,10,2,3\).
Mathematica
In Mathematica, standard Mathematica matrix operations can be used, or the matrix can be converted to SparseArray and special named operation can be used on it.
Matlab A=[0 0 1; 10 0 2; 3 4 0]; values= nonzeros(A)
|
values = 10 3 4 1 2
|
%find locations [I,J]=find(A)
|
I = 2 3 3 1 2 J = 1 1 2 3 3 |