for loop stack the data

17 views (last 30 days)
chang hoon oh
chang hoon oh on 9 Sep 2021
Answered: Jan on 9 Sep 2021
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?

Accepted Answer

Mathieu NOE
Mathieu NOE on 9 Sep 2021
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

More Answers (1)

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

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!