Borrar filtros
Borrar filtros

How to access a cell array using loops?

2 visualizaciones (últimos 30 días)
DEEPAK PHCSFI17041149
DEEPAK PHCSFI17041149 el 18 de Abr. de 2018
Comentada: DEEPAK PHCSFI17041149 el 20 de Abr. de 2018
I have a cell array A who's dimensions is in multiples of 8. For ex 1x192 , now i want to pick the elements of the cell array in the following way.
1,9,17,25...nth elements should be stored it in a separate cell array. similarly 2,10,18,26..n in a separate cell array and another set would be 3,11,19,27..n in a separate cell array. this must be continued till 8,16,24,32..
How can i automate this, where the cell array size is dynamic. But it is always a multiple of 8.
Any help would be appreciated thanks

Respuesta aceptada

Stephen23
Stephen23 el 18 de Abr. de 2018
Editada: Stephen23 el 18 de Abr. de 2018

Storing data in lots of separate variables should be avoided, read this to know why:

https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval

The simplest solution would be for you to reshape your array into a matrix and access its rows/columns:

C = cell(1,192) % your cell array
M = reshape(C,8,[]);

then accessing the data that you require is trivial using basic MATLAB indexing:

M(1,:) % 1, 9,17,...
M(2,:) % 2,10,18,...
...
M(8,:) % 8,16,24,...

Note that you can easily access the data in a loop:

for k = 1:8
    M(k,:)
end

or split it into a cell array of cell arrays using num2cell.

  1 comentario
DEEPAK PHCSFI17041149
DEEPAK PHCSFI17041149 el 20 de Abr. de 2018
Great Use of Re-Use function, thanks much Stephen.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrices and Arrays en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by