How to sort large data from a cell array
13 views (last 30 days)
Show older comments
I have seen many post on here that clarity is often lacking, and I will try to be as clear as possible with my explanation.
I have a cell array: datamatrix 1x10x84 cell
The cell array is divisible by 14, thus:
datamatrix(1) 1x10x1:6
datamatrix(2) 1x10x7:12
....
datamatrix(14) 1x10x79:84
I want to write a loop that will group them for me, however I don't quite no where to start.
the output will be, for example:
datamatrix(1) 1x10x1:6 = Group1 1x10x6
And so on until the end of the cell array.
I know I can do this manually, however I was hoping for some assistance with constructing a loop of some sort, or a point into the right direction where I can work this out.
Many thanks in advance.
6 Comments
Stephen23
on 14 Apr 2017
Edited: Stephen23
on 14 Apr 2017
@Theodore Bowen: creating or accessing numbered variables (e.g. Fish1, Fish2, etc) will be slow, buggy, and obfuscated code. Although popular with beginners, it is not recommended by The MathWorks (who write MATLAB), or any of the experts on this forum:
The much simpler (neater, faster, more efficient) solution is to use indexing (e.g. Fish{1}, Fish{2}, etc). My answer already shows you how to do this.
Answers (1)
Stephen23
on 11 Apr 2017
Edited: Stephen23
on 12 Apr 2017
C = squeeze(mat2cell(D,1,10,6*ones(1,14)));
Where D is your cell matrix, and the squeeze is optional. Note that the more nested cell arrays you have the more difficult it is to access your data, the more complicated your code will be, and the more bugs it will have. Personally I would ask the exact opposite of this question: "how can I join my data together as much as possible?"
2 Comments
Stephen23
on 12 Apr 2017
Edited: Stephen23
on 12 Apr 2017
"You are saying then, bring all the data together, into a matrix, and then separate accordingly?"
Not at all. D is your cell matrix. You do not need to "bring anything together".
"This method would still need a code/loop to sort the data into groups."
Not at all. My code splits your cell matrix into 14 separate cell matrices (i.e. your groups), which is exactly what you asked for. Why would I write an answer and not mention many critical steps?
I showed you one easy way to split your cell matrix in groups (this is what you asked for). The method I showed works, it does not require any interpretation, loops, sorting, bringing data together, or anything other complications. I do not give partial answers, and I do not give untested answers, and I would have shown/explained if any other steps were required.
Here is a full working example, showing how simple it really is:
>> D = num2cell(randi(9,1,10,84)); % 1x10x84 cell
>> C = squeeze(mat2cell(D,1,10,6*ones(1,14))); % 14x1 cell
>> size(C{1}) % the first group
ans =
1 10 6
>> size(C{2}) % the second group
ans =
1 10 6
... etc
So C simply contains the groups that you ask for.
See Also
Categories
Find more on Performance and Memory in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!