for loop stack the data

5 次查看(过去 30 天)
i made a code which can detect the specific number's position in array
like this
for k=77:176;
[col,row]=find(M==k);
end
but the data didn't stack the whole data. after running the code only show the 176's data. how can i get the whole data?

采纳的回答

Mathieu NOE
Mathieu NOE 2021-9-9
hello
my suggestion
if M is (like here) a 1D array so the col saved is always one; this can be removed from the code if needed.
M = 1:10:1000;
ind = 0;
for k=77:176
[col,row] = find(M==k);
if ~isempty(col) & ~isempty(row)
ind = ind +1;
k_saved(ind) = k;
col_saved(ind) = col;
row_saved(ind) = row;
end
end
if M is a 2D array , see this : (same code in fact , but now col is not only one of course)
M = 71:1:170;
M = reshape(M,10,10);
ind = 0;
for k=77:176
[col,row] = find(M==k);
if ~isempty(col) & ~isempty(row)
ind = ind +1;
k_saved(ind) = k;
col_saved(ind) = col;
row_saved(ind) = row;
end
end

更多回答(1 个)

Jan
Jan 2021-9-9
Maybe:
col = cell(1, 176);
row = cell(1, 176);
for k = 77:176
[col{k}, row{k}] = find(M == k);
end

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by