how to make a loop for condition?

2 visualizaciones (últimos 30 días)
Fabyola
Fabyola el 22 de Jun. de 2021
Comentada: Stephen23 el 22 de Jun. de 2021
how to make this more efficient? i've done clustering, so i want to separate them based on their cluster, so i made like this, but if i change the r so the number of their cluster will change, so i want to they loop based on their number of the cluster, i've tried use for, but i didn't found out how to make loop for condition like this
ind1 = idx == 1;
ind2 = idx == 2;
ind3 = idx == 3;
ind4 = idx == 4;
ind5 = idx == 5;
ind6 = idx == 6;
C1_k = datareal(ind1,:);
C2_k = datareal(ind2,:);
C3_k = datareal(ind3,:);
C4_k = datareal(ind4,:);
C5_k = datareal(ind5,:);
C6_k = datareal(ind6,:);
  1 comentario
Stephen23
Stephen23 el 22 de Jun. de 2021
"...i didn't found out how to make loop for condition like this"
Meta-data in variable names indicates that you are doing something wrong.
Copy-and-pasting code like that indicates that you are doing something wrong.
The neat, simple, efficient MATLAB approach is to use indexing with one array.

Iniciar sesión para comentar.

Respuesta aceptada

KSSV
KSSV el 22 de Jun. de 2021
G = 6 ; % number of cluster
ind = cell(G,1) ;
C = cell(G,1) ;
for i = 1:G
ind{i} = idx == i;
C{i} = dataarea1(idx==i,:) ;
end
  1 comentario
Fabyola
Fabyola el 22 de Jun. de 2021
aah i see, okaay thanks a lot for your help

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by