for loop stack the data

11 visualizaciones (últimos 30 días)
chang hoon oh
chang hoon oh el 9 de Sept. de 2021
Respondida: Jan el 9 de Sept. de 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?

Respuesta aceptada

Mathieu NOE
Mathieu NOE el 9 de Sept. de 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

Más respuestas (1)

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

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by