Find the lowest location within rows where there are non-zero elements for each column in a matrix in MATLAB -
for example, have 4x6 matrix a:
a = 0 0 0 0 4 3 0 2 1 0 0 0 0 5 0 8 7 0 8 9 10 3 0 2
i want find lowest location within rows of non-zero elements found each column. should go this:
column 1 => row 4 column 2 => row 2 column 3 => row 2 column 4 => row 3 column 5 => row 1 column 6 => row 1
and result should following vector:
result = [4, 2, 2, 3, 1, 1]
anyone has idea how obtain this?
this should it:
a = [0, 0, 0, 0, 4, 3; 0, 2, 1, 0, 0, 0; 0, 5, 0, 8, 7, 0; 8, 9, 10, 3, 0, 2]; indices = repmat((1:size(a))', [1, size(a, 2)]); indices(a == 0) = nan; min(indices, [], 1)
here indices is:
indices = 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 4 4 4 4 4 4
we set every element of indices
nan
wherever zero, gives us:
indices = nan nan nan nan 1 1 nan 2 2 nan nan nan nan 3 nan 3 3 nan 4 4 4 4 nan 4
we take minimum of each column
Comments
Post a Comment