How to sort large data from a cell array

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

I presume that the "sort" term in the title is a misnomer, because the description does not describe sorting the data at all, but rather the splitting of data into submatrices.
While thank you for the assistance, the tone of superiority is not appreciated. Not all of us have utilised matlab to the extent of others, or programming, thus I can only apologise for the use of the incorrect terminology.
Stephen23
Stephen23 on 12 Apr 2017
Edited: Stephen23 on 12 Apr 2017
"I can only apologise for the use of the incorrect terminology."
My comment was to clarify your question, which we do here in order to be able to help you.
We get thousands of questions from beginners, who often use non-standard terminology: as a beginner this is reasonable because terminology has to be learned along with the rest of MATLAB. However the volunteers here (trying to help people) have to understand what those posters are talking about or requesting. As a result we spend quite a large amount of time just trying to figure out what posters want, and asking for clarifications.
Sometimes we clarify something, and the poster then makes corrections: perhaps you did want a sort: it is possible that you wanted to both sort and split your data: you might know what you want, but I can only figure it out from your words. And from your description it did not seem likely, which is why I make that comment. If I was incorrect you would be free to point this out: "no, I really do want to sort the data as well...". So by trying to clarify your task it helps you because then you get what you want.
Posters often request something specific but actually want something different (like you did). Oftentimes this is not intentional (like in your case). But this does not remove our need to use a common language and description: through accepted terminology we can communicate ideas most efficiently, and can then convert this into a technically feasible solution. That is why I pointed out the incorrect term. However using a wrong term is not your fault, and certainly does not deserve any apology!
@Theodore: The frequent contributors in this forum like to assist others to solve Matlab problems. Questions for clarifications are usually required, because it is a main task of solving a problem to find out, what the problem exactly is. This is no kind of "superiority".
I do not understand the formulations:
The cell array is divisible by 14, thus:
datamatrix(1) 1x10x1:6
and
datamatrix(1) 1x10x1:6 = Group1 1x10x6
Can you create some Matlab code, which produces these arrays manually?
@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

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.
"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

Asked:

on 11 Apr 2017

Edited:

on 14 Apr 2017

Community Treasure Hunt

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

Start Hunting!