Effacer les filtres
Effacer les filtres

for loop stack the data

5 vues (au cours des 30 derniers jours)
chang hoon oh
chang hoon oh le 9 Sep 2021
Réponse apportée : Jan le 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?

Réponse acceptée

Mathieu NOE
Mathieu NOE le 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

Plus de réponses (1)

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

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by