Let
m={{1, 2, 0}, {4, 0, 9}};
I want to find the position of every nonzero element of the above matrix. The code
SparseArray[m]["NonzeroPositions"]
returns
{{1, 1}, {1, 2}, {2, 1}, {2, 3}}
I want the output will be something like this
{{1, 2}, {1, 3}}
that is every element of the output is a list of the corresponding columns of nonzero elements in each row. How do I modify the code?
GatherBy[SparseArray[m]["NonzeroPositions"], First][[All, All, 2]]
$\endgroup$(SparseArray[#]["NonzeroPositions"] & /@ m)[[;; , ;; , 1]]
$\endgroup$