Info

# A row vector that matches all similar rows in a matrix to group be them in a scatter plot

1 view (last 30 days)
Yaser Khojah on 20 Mar 2019
Closed: MATLAB Answer Bot on 20 Aug 2021
I have a huge matrix where I want to split them into smaller group based on certain criteria where all the rows are matching. For simplicity I have created the following example to demonstrate what I want to do as below
%The main Matrix
A = [1 1 1 40 50; 0 0 0 60 40; 2 2 2 20 10; 3 3 3 40 30; 1 1 1 60 70; 0 0 0 20 10];
% find the unique values as below
B = unique(A(:,1:3),'row');
[Lia,Locb] = ismember(A(:,1:3),B,'row');
% create the New Mat
NewMat = [A , Locb];
NewMat =
1 1 1 40 50 2
0 0 0 60 40 1
2 2 2 20 10 3
3 3 3 40 30 4
1 1 1 60 70 2
0 0 0 20 10 1
I'm looking for a way to split the new matrix (NewMat) into new matrixes based on their Locb values which are located in column 6. To get the following:
NewMat_1 = [ 0 0 0 60 40 1; 0 0 0 20 10 1]
NewMat_2 = [ 1 1 1 40 50 2; 1 1 1 60 70 2]
NewMat_3 = [ 2 2 2 20 10 3 ]
NewMat_4 = [ 3 3 3 40 30 4 ]
Then plot them all in one scatter plot with different group color
scatter(NewMat_1(:,4), NewMat_1(:,4)); hold on
scatter(NewMat_2(:,4), NewMat_1(:,4)); hold on
scatter(NewMat_3(:,4), NewMat_1(:,4)); hold on
scatter(NewMat_4(:,4), NewMat_1(:,4)); hold on
legend('1','2','3','4')
madhan ravi on 21 Mar 2019
Please explicitly show how your m should look like to get a clear picture of your goal?

Jan on 21 Mar 2019
Edited: Jan on 21 Mar 2019
A = [1 1 1 40 50; 0 0 0 60 40; 2 2 2 20 10; 3 3 3 40 30; 1 1 1 60 70; 0 0 0 20 10];
B = unique(A(:,1:3),'row');
[Lia,Locb] = ismember(A(:,1:3),B,'row');
NewMat = [A , Locb];
M = splitapply(@(x){x}, NewMat, Locb)
By the way, this can be improved:
row = find(NewMat(:,6)==Un_Locb(i));
% ^^^^ omit this for faster logical indexing
M{i} = NewMat(row,:);