TiredHornet
TiredHornet

Reputation: 49

Create group identifier from two vectors in matlab

I would like to create a unique group identifier vector (G) based on the values in two column vectors (A and B).

A = [1; 1; 1; 2; 2; 1; 1; 2; 2]
B = [1; 1; 2; 1; 2; 1; 1; 1; 2]

I would like G to look like this:

G = [1; 1; 2; 3; 4; 1; 1; 3; 4]

This is probably something simple, but I just can't seem to find the command(s) to do this.

Upvotes: 0

Views: 160

Answers (1)

Divakar
Divakar

Reputation: 221614

Simple indeed. You need to use unique(...'rows') on vertically stacked input vectors and the third output from it would be your desired output, like so -

[~,~,G] = unique([A(:) B(:)],'rows')

Upvotes: 1

Related Questions