MATLAB: Storing the index of an Matrix for non zeros

indexingmatlab function

i want to store the index values of non zero values in a vector (row instead of col )

Best Answer

In just two lines, no loop required:
>> X = [1,0,2;0,1,1;0,0,4]
X =
1 0 2
0 1 1
0 0 4
>> [C,R] = find(X.');
>> Z = accumarray(R,C,[],@(v){v});
And checking:
>> Z{:}
ans =
1
3
ans =
2
3
ans =
3