for loop stack the data

11 visualizzazioni (ultimi 30 giorni)
chang hoon oh
chang hoon oh il 9 Set 2021
Risposto: Jan il 9 Set 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?

Risposta accettata

Mathieu NOE
Mathieu NOE il 9 Set 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

Più risposte (1)

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

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Tag

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by