Вопрос

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

Это было полезно?

Решение 2

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'

Другие советы

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top