How do i get multiple values all at once from cell array?

Hi,
I have a 7365x60 cell, and there are 1x7 cell in every single block of the big one, like the image shown below:
here is the question, how can i get the second value from the 1x7 cell all at once without using cellfun and for loop(cause they are slow), for example, i want to get f from the Trajectory_Data (has 7365x60 cell) which is shown below, but when i run the code below, i got the error:"Expected one output from a curly brace or dot indexing expression, but there were 7365 results.",i dont know how to get all the second value from the cell (there would be 7365x60 = 441900 values in total),can anyone help me?
f = Trajectory_Data{:,1}{:,2}

Respuestas (3)

KSSV
KSSV el 29 de Jun. de 2022
Editada: KSSV el 29 de Jun. de 2022
Let A be your cell array of size 7365x60 where each cell has 1x7 array.
B = cell2mat(A) ;
iwant = B(:,2:7:end) ;

5 comentarios

i tried this one before, and i got 'cell2mat does not support cell arrays xontaining cell arrays or objects'.
KSSV
KSSV el 29 de Jun. de 2022
Editada: KSSV el 29 de Jun. de 2022
What each cell 1x7 has?
If you are running a loop are you intiailizng it? Loop will be faster, it wont be slow.
"What each cell 1x7 has?" doesn't really matter:
C = {{1,2,3};{4,5,6};{7,8,9}}
C = 3×1 cell array
{1×3 cell} {1×3 cell} {1×3 cell}
D = cell2mat(C)
Error using cell2mat
CELL2MAT does not support cell arrays containing cell arrays or objects.
Ohhh yes.....I took it as:
C = {[1,2,3];[4,5,6];[7,8,9]} ;
D = cell2mat(C)
D = 3×3
1 2 3 4 5 6 7 8 9
thx KS, i got some idea from this one

Iniciar sesión para comentar.

Walter Roberson
Walter Roberson el 29 de Jun. de 2022
Your options:
  • loop
  • cellfun
  • arrayfun (effectively the same as cellfun)
  • cell2mat to form a 3d array, and then index; this is very likely going to be slower than cellfun
  • cat() the contents of the cell together along some dimension, process the results, and reshape() afterwards; this is very likely going to be slower than cellfun
Stephen23
Stephen23 el 29 de Jun. de 2022
Editada: Stephen23 el 29 de Jun. de 2022
"... for loop(cause they are slow)"
A well-written loop will be fast enough, e.g. where C is your cell array and assuming that the nested cells contain scalar numerics:
M = nan(size(C));
for k = 1:numel(C)
M(k) = C{k}{2};
end
Better data design would store such regular data in one numeric array, which would make this task trivial and fast.

3 comentarios

is the 'C' refers to Trajectory_Data?
Stephen23
Stephen23 el 29 de Jun. de 2022
Editada: Stephen23 el 29 de Jun. de 2022
yes, just use the name of your cell array.
Thx, i'll try this later

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Preguntada:

el 29 de Jun. de 2022

Editada:

el 29 de Jun. de 2022

Community Treasure Hunt

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

Start Hunting!

Translated by