How to sort large data from a cell array

13 views (last 30 days)
Theodore Bowen
Theodore Bowen on 11 Apr 2017
Edited: Stephen23 on 14 Apr 2017
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
Theodore Bowen
Theodore Bowen on 13 Apr 2017
Edited: Theodore Bowen on 13 Apr 2017
@Jan. It was the tone that is all. It has been cleared anyhow. To your point. In quotation is what I originally posted.
"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"
To answer your query, the third dimension of the datamatrix is 84. Each group consists of 6 in the 3rd dimension, as i hope clearly represented above. Thus, 84/14 = 6. Apologies for my description. I meant 6, due to each group containing 6 3rd dimensions. Therefore, 14 groups. Apologies.
I have created a code to do this manually, but I was hoping there is a way to construct a loop, because my third dimension will increase to 840. And I will need to catergorise them as I have done manually in the code. 14 grouped data turns to 140, which as you can imagine will take a long time to type out.
Is this clear?
Fish1 = datamatrix(1,10,1:6);
Fish2 = datamatrix(1,10,7:12);
Stephen23
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.

Sign in to comment.

Answers (1)

Stephen23
Stephen23 on 11 Apr 2017
Edited: Stephen23 on 12 Apr 2017
You could use mat2cell:
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
Theodore Bowen
Theodore Bowen on 12 Apr 2017
Thank you, I will give it a try now.
You are saying then, bring all the data together, into a matrix, and then separate accordingly? This method would still need a code/loop to sort the data into groups.
Stephen23
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.

Sign in to comment.

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!