Reputation: 365
I have a situation like this
I have data M size (x by 2) where data were in already in classify group by
C = unique(M(:) , 'rows'); %result will be in g-by-2
now I want to assign the data group M accord to C
by creating a R matrix size M-by-1.
Example
M = [ 1 2;
3 3;
1 2;
1 5;
. . ];
assume I got 3 groups
C = [ 1 2;
3 3;
1 5];
I want R to be like
R = [ 1;
2;
1;
3;
. ];
I try to use loop for and find to compare all group
for i = 1:size(C(1)
find(M(:) == C(i,:));
end
but it didn't work
Upvotes: 1
Views: 96
Reputation: 36720
The problem is, you are using the input argument M(:)
, this is no longer a matrix. Use [A,B,C]=unique(M , 'rows')
instead.
Besides this the order of your expected output requires the option 'stable'
Upvotes: 1
Reputation: 6434
No need to find the group numbers. You should do this:
M = [ 1 2;
3 3;
1 2;
1 5;
];
[CC,ia,ic] = unique(M,'rows')
CC =
1 2
1 5
3 3
ia =
3
4
2
ic =
1
3
1
2
ic is what you are looking for.
Upvotes: 2