Convert cell array to set of matrices
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
Walter Roberson
2022-6-10
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);
Stephen23
2022-6-10
编辑:Stephen23
2022-6-10
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?
回答(1 个)
Studentskp
2022-6-12
4 个评论
Walter Roberson
2022-6-13
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?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Resizing and Reshaping Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!