Info

此问题已关闭。 请重新打开它进行编辑或回答。

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

1 次查看(过去 30 天)
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 个评论
Yaser Khojah
Yaser Khojah 2019-3-21
编辑:Yaser Khojah 2019-3-21
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.

回答(1 个)

Jan
Jan 2019-3-21
编辑:Jan 2019-3-21
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,:);

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by