找到你要的答案

Q:Get row/column index while looping with numel

Q:而循环numel得到行/列的索引

I am just approaching Matlab, is there a way to get the row- and column-indices from i while looping with numel? I mean for each cycle 1:numel(B), get the relevant variable for the value of row and column:

for i = 1:numel(B)
 rowIndex= ??
 colIndex= ??

    ...my stuff...

 end

I am just approaching Matlab, is there a way to get the row- and column-indices from i while looping with numel? I mean for each cycle 1:numel(B), get the relevant variable for the value of row and column:

for i = 1:numel(B)
 rowIndex= ??
 colIndex= ??

    ...my stuff...

 end
answer1: 回答1:

You can use ind2sub which converts a linear index i to row and column subscripts given the size of the matrix:

[rowIndex, colIndex] = ind2sub(size(B), i)

Because ind2sub can return multiple values, the row index is assigned directly to rowIndex and the column to colIndex.

你可以使用ind2sub将线性指数我行和列下标给定矩阵的大小:

[rowIndex, colIndex] = ind2sub(size(B), i)

因为ind2sub可以返回多个值的行的索引是直接分配到开始和柱colindex。

matlab