## How can I make smaller matrices (size unknown) from a large matrix?

### John Hunt (view profile)

on 3 Oct 2017
Latest activity Commented on by Cedric Wannaz

on 4 Oct 2017

### James Tursa (view profile)

I have a matrix that has 51 columns and 46999 rows. The 8th column has values 1 to 36. I want to create a matrix for each value in that column (36 total). I want to stack rows that have the same value to make these matrices.
Thus far I know can use a for loop to set up a matrix 36 times then I was trying to use a while loop to "stack" the rows that share the same value in the 8th column, but I can't get that while loop right. Any suggestions?

John Hunt

### John Hunt (view profile)

on 4 Oct 2017
That worked awesome! Thank you, could you explain it a bit so I can understand a bit better whats going on please?

### James Tursa (view profile)

on 3 Oct 2017

result = cellfun(@(x)M(M(:,8)==x,:),num2cell(1:36),'uni',false);

Cedric Wannaz

### Cedric Wannaz (view profile)

on 4 Oct 2017
I was just being fair, because if we are both roughly in the same timezone, it is getting late (!) and the probability that we implement extra calls to num2cell induced by an inadvertent use of cellfun is nearing the unit ;)
Jan

### Jan (view profile)

on 4 Oct 2017
+1 for both of you.
@Cedric: Now you know the reason, why I "boost" sometimes. It is for the cases, where the standard voting system is to rough. :-)
Cedric Wannaz

### Cedric Wannaz (view profile)

on 4 Oct 2017
:-)
It has already been proposed, but I really think that allowing voting for comment would help. This would lead people to add much more value to others' answers by taking time to write well developed comments.