how to access cell array data with single for loop

1 visualización (últimos 30 días)
singh
singh el 27 de Abr. de 2015
Comentada: DEEPAK PHCSFI17041149 el 29 de Nov. de 2017
two cell array like
A={1,2,3,4,5} %cell array
B={11,12,13,14,15} %cell array
for i=1:length(A)
C %variable
D %variable
end
in first iteration C take 1 from A and D take 11 from B
second iteration C take 2 from A and D take 12 from B
So on
use only single for loop
  2 comentarios
Stephen23
Stephen23 el 27 de Abr. de 2015
Editada: Stephen23 el 27 de Abr. de 2015
Why are these values in cell arrays anyway? Unless you have mixed data types or different sizes, just use a simple numeric array to store those values.
Also note that, depending on what operations you need to perform, the entire loop might be able to be replaced with vectorized code. If you show us what the operations are inside the loop, then we could show you if this can be simplified by vectorization.
singh
singh el 27 de Abr. de 2015
i have some problem because actual cell array value is below
A{:}
17
6 11
1 9
B{:}
1 9
6 11
17

Iniciar sesión para comentar.

Respuesta aceptada

per isakson
per isakson el 27 de Abr. de 2015
Editada: per isakson el 27 de Abr. de 2015
C = cell( size(A));
D = cell( size(B));
for ii=1:length(A)
C(ii) = A(ii);
D(ii) = B(ii);
end
&nbsp
Addendum
Without the loop - vectorized
C = A;
D = B;
  4 comentarios
DEEPAK PHCSFI17041149
DEEPAK PHCSFI17041149 el 29 de Nov. de 2017
Thanks for the guidance Isak.

Iniciar sesión para comentar.

Más respuestas (1)

Michael Haderlein
Michael Haderlein el 27 de Abr. de 2015
You mean like
for cnt=1:length(A)
C=A{cnt};
D=B{cnt};
end
Actually, there is no need for cell arrays here. In case this is just a simplified example and A and B will be of different kind in your final program, it's fine. Otherwise, I would make A and B just normal double arrays.

Categorías

Más información sobre Loops and Conditional Statements 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