# how to split 6x4 double based on first columns values into three [2X4]?

1 view (last 30 days)
MS on 16 Dec 2022
Answered: Matt J on 16 Dec 2022
4 1 2 3
4 5 6 7
5 1 2 3
5 4 5 6 ====>> [4 1 2 3 [ 5 1 2 3 [6 1 2 3
5 4 5 6
5 4 5 6 4 5 6 7] 5 4 5 6] 6 4 5 6]
6 1 2 3
6 4 5 6

Matt J on 16 Dec 2022
A=[4 1 2 3
4 5 6 7
5 1 2 3
5 4 5 6
5 4 5 6
5 4 5 6
6 1 2 3
6 4 5 6];
A=unique(A,'rows');
Acell=splitapply(@(x){x}, A,findgroups(A(:,1)));
Acell{:}
ans = 2×4
4 1 2 3 4 5 6 7
ans = 2×4
5 1 2 3 5 4 5 6
ans = 2×4
6 1 2 3 6 4 5 6

Matt J on 16 Dec 2022
Edited: Matt J on 16 Dec 2022
A=randi(9,6,4)
A = 6×4
6 1 9 7 8 5 8 9 9 7 9 9 6 4 1 4 6 7 7 9 3 8 1 2
Acell=mat2tiles(A,[2,4]); %get mat2tiles from https://www.mathworks.com/matlabcentral/fileexchange/35085-mat2tiles-divide-array-into-equal-sized-sub-arrays
Acell{:}
ans = 2×4
6 1 9 7 8 5 8 9
ans = 2×4
9 7 9 9 6 4 1 4
ans = 2×4
6 7 7 9 3 8 1 2
MS on 16 Dec 2022
Edited: MS on 16 Dec 2022
Thanks @Matt J sorry for the mis commmunciation. if you see the three splitted/grouped matrices are based on the fitst column values. I mean, it should be grouped based on the first column values such as 4, 5, 6.
All matrices should be grouped based on the first column values. 1, all matcies with 4 should be sperate matrix 2, all matrix with 5 should be sperate matrix 3, all matcies with 6should be sperate matrix etc.,
my matrix is not random.