For an arbitrary sized matrix x, how do I find the index of the last non-zero element in each row of a given matrix?
x
For example, for the matrix
My answer's a bit twisted but it should work too
x = [ 0 9 7 0 0 0; 5 0 0 6 0 3; 0 0 0 0 0 0; 8 0 4 2 1 0 ]; [~,pos] = max([fliplr(x~=0),ones(size(x,1))],[],2); v = size(x,2)-pos' +1;