matlab - Tool to work with indices in matrix -
i have square matrix elements, can take values {-1, 0, 1}. know there 1 element equals -1 in every row (except first row, non-negative elements).
let n size of matrix. so, want assign every index i [2, n] position of -1 in i-th row matrix.
how can without using for or while loop?
t = matrix==-1; [~, pos] = max(t,[],2); %// position of first -1 within each row. pos = pos .* any(t,2); %// give 0 rows don't contain -1 value pos(k) gives position of (first) -1 value within row k, or 0 if there isn't -1 value in row.
example:
matrix = 1 0 0 1 0 1 -1 1 0 1 1 -1 0 0 1 -1 gives
pos = 0 3 4 4
Comments
Post a Comment