Convert cell array to set of matrices
Mostrar comentarios más antiguos
I am trying to convert a cell aray into a 30 set of matrices orsmaller arrays of size 100 x 2. I need to sort these matrices (using 'sortrows' and get the mean of each row later).
Cell array (say C) size is 1 x 30, each cell is 100 x 2
I am trying to use a for loop for this.
for ii =1:30
M(ii) =C{ii};
end
I get an error message saying "indices on left side are not compatible with the size of right side". Any ideas about how to solve this?
Thanks,
2 comentarios
Walter Roberson
el 10 de Jun. de 2022
Why do you want to do this? Why not just leave them as cell array?
sC = cellfun(@sortrows, C, 'UniformOutput', 0);
mr = cellfun(@(M) mean(M, 2), sC, 'UniformOutput', 0);
So you are trying to create lots of dynamically-named variables in the workspace, thus forcing yourself into writing slow, complex, inefficient, obfuscated, buggy code that is hard to debug:
"Any ideas about how to solve this? "
Use indexing and your cell array. Indexing is neat, simple, and very efficient (unlike what you are trying to do).
"I need to sort these matrices (using 'sortrows' and get the mean of each row later)."
You already have a loop and use indexing: what is stopping you from sorting the matrices inside that loop?
Respuestas (1)
Studentskp
el 12 de Jun. de 2022
0 votos
4 comentarios
Walter Roberson
el 12 de Jun. de 2022
sC = cellfun(@sortrows, C, 'UniformOutput', 0);
woulkd take your 1 x 30 cell, C, and return sC, which would be a 1 x 30 cell in which each 100 x 2 had had sortrows() applied to it. No need to create 30 array.
I want to bring them into one array, without changing the actual time of observation. So, my array will have one 'time' column and 30 columns of obervations. Length of the columns will not be 10 anymore.
What would that look like to you?
%T {1,1}(2) {1,2}(2) {1,3}(2)
0.001 0.837 nan nan %{1,2}, {1,3} start after 0.001
0.002 0.842 0.755 nan %{1,1}, {1,2} have 0.002 readings but {1,3} has not started
0.003 0.848 nan nan %{1,2}, {1,3} do not have any 0.003 reading
0.0033 nan nan 0.649 %{1,3} has a 0.0033 reading but nothing else does
Or... should the code find a list of all times that exist in at least one cell, and interpolate the signals at all of those times, but have nan for entries before or after the time range defined for that cell?
Or...should the code find a list of all times that exist in at least one cell, and interpolate and extrapolate (linear? spline?) to cover the entire range?
Or... should the code use fixed timesteps 0.001 : 0.001 : last and interpolate and extrapolate values at the fixed timesteps based upon the available data for each cell ?
Studentskp
el 13 de Jun. de 2022
Walter Roberson
el 13 de Jun. de 2022
To confirm: you want to create an array in which every time used in any cell is in the time vector, and for each file if that exact time appears then the value should be filled in, but any cell that does not have that exact time should have nan?
Studentskp
el 13 de Jun. de 2022
Categorías
Más información sobre Resizing and Reshaping Matrices en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!