Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Compute order of occurrence for each unique value

Subject: Compute order of occurrence for each unique value

From: Hoi Wong

Date: 3 Apr, 2012 18:56:13

Message: 1 of 3

I have a groupVar (grouping) vector which the elements represents the group#, and for each element (group#), I want to associate it with the order (1,2,3,4...) which that group# occurred.

Here's an example (To make it less likely to confuse with the occurence , groupVar is multiplied by 10):

[groupVar, occurrenceOrder] =
40 1
10 1
20 1
40 2
20 2
50 1
40 3
50 2
50 3
50 4

If the groupVar is sorted (so it's easier to read):
[groupVar, occurrenceOrder] =
40 1
40 2
40 3
10 1
20 1
20 2
50 1
50 2
50 3
50 4

I've actually written code to calculate the number of occurrences (hist, unique), but I don't want to go through each unique value and assign it to [1:count(k)]'. Can anybody suggest a neat way to do it?

Thanks.

Subject: Compute order of occurrence for each unique value

From: Roger Stafford

Date: 3 Apr, 2012 20:17:18

Message: 2 of 3

"Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <jlfh4d$g56$1@newscl01ah.mathworks.com>...
> I have a groupVar (grouping) vector which the elements represents the group#, and for each element (group#), I want to associate it with the order (1,2,3,4...) which that group# occurred.
>
> Here's an example (To make it less likely to confuse with the occurence , groupVar is multiplied by 10):
>
> [groupVar, occurrenceOrder] =
> 40 1
> 10 1
> 20 1
> 40 2
> 20 2
> 50 1
> 40 3
> 50 2
> 50 3
> 50 4
>
> If the groupVar is sorted (so it's easier to read):
> [groupVar, occurrenceOrder] =
> 40 1
> 40 2
> 40 3
> 10 1
> 20 1
> 20 2
> 50 1
> 50 2
> 50 3
> 50 4
>
> I've actually written code to calculate the number of occurrences (hist, unique), but I don't want to go through each unique value and assign it to [1:count(k)]'. Can anybody suggest a neat way to do it?
>
> Thanks.
- - - - - - - -
  Here's one way to do that. I use 'g' for 'groupVar' and 'o' for 'occurrenceOrder'. Assume 'g' is a column vector.

 [o,p] = sort(g);
 f = find([true;diff(o)~=0]);
 q = ones(size(g));
 q(f(2:end)) = 1-diff(f);
 o(p) = cumsum(q);

Roger Stafford

Subject: Compute order of occurrence for each unique value

From: Hoi Wong

Date: 3 Apr, 2012 21:37:16

Message: 3 of 3

"Roger Stafford" wrote in message <jlflse$3k2$1@newscl01ah.mathworks.com>...
> "Hoi Wong" <wonghoi.ee@gmailNOSPAM.com> wrote in message <jlfh4d$g56$1@newscl01ah.mathworks.com>...
> > I have a groupVar (grouping) vector which the elements represents the group#, and for each element (group#), I want to associate it with the order (1,2,3,4...) which that group# occurred.
> >
> > Here's an example (To make it less likely to confuse with the occurence , groupVar is multiplied by 10):
> >
> > [groupVar, occurrenceOrder] =
> > 40 1
> > 10 1
> > 20 1
> > 40 2
> > 20 2
> > 50 1
> > 40 3
> > 50 2
> > 50 3
> > 50 4
> >
> > If the groupVar is sorted (so it's easier to read):
> > [groupVar, occurrenceOrder] =
> > 40 1
> > 40 2
> > 40 3
> > 10 1
> > 20 1
> > 20 2
> > 50 1
> > 50 2
> > 50 3
> > 50 4
> >
> > I've actually written code to calculate the number of occurrences (hist, unique), but I don't want to go through each unique value and assign it to [1:count(k)]'. Can anybody suggest a neat way to do it?
> >
> > Thanks.
> - - - - - - - -
> Here's one way to do that. I use 'g' for 'groupVar' and 'o' for 'occurrenceOrder'. Assume 'g' is a column vector.
>
> [o,p] = sort(g);
> f = find([true;diff(o)~=0]);
> q = ones(size(g));
> q(f(2:end)) = 1-diff(f);
> o(p) = cumsum(q);
>
> Roger Stafford

Neat! Thanks a lot.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us