Info

This question is closed. Reopen it to edit or answer.

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

1 view (last 30 days)
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')
Anyway to help please.
  4 Comments
Yaser Khojah
Yaser Khojah on 21 Mar 2019
Edited: Yaser Khojah on 21 Mar 2019
Hello Madhan, M can be the result or any other way to get all the rows that in A (:,1:3) share similar values as in B. ismember function shows only one row index and does not show the rest. I want to see all the indexes in an efficient way rather than using find function since I have a huge matrix.

Answers (1)

Jan
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,:);

Tags

Community Treasure Hunt

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

Start Hunting!