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
One-line solution with bsxfun:
result = max(bsxfun(@times, x~=0, 1:size(x,2)).');
Or use the two outputs of max:
[val, result] = max(fliplr(x~=0).',[],1); %' result = (size(A,2)+1-result).*val;