Got Questions? Get Answers.
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:
Finding the number of match data

Subject: Finding the number of match data

From: Lin

Date: 24 Mar, 2009 12:05:05

Message: 1 of 4

Hi all.

Say I have something as shown:

x' =
2
3
4
5
2
6
7
3
4

How can i find the number of match data? I try to do some elaboration. i take the first value '2' and compare against the rest of the data to find a match. If yes, counter + 1. Then i take the next value '3' and do the comparison again.

Expected result:

THere are 3 sets of match data, 2, 3 and 4.

I could not achieve the results using the unique function, because it reduces to something like
 2
 3
 4
 5
 6
 7

I tried using ismember too, but the code is not so dynamic, since i need to makes several changes when i change the input data.

Is there any way where i can achieve my expected results? Thanks for helping.

Subject: Finding the number of match data

From: Bruno Luong

Date: 24 Mar, 2009 12:17:02

Message: 2 of 4

"Lin " <shenqlv@yahoo.com> wrote in message <gqai9h$66b$1@fred.mathworks.com>...
> Hi all.
>
> Say I have something as shown:
>
> x' =
> 2
> 3
> 4
> 5
> 2
> 6
> 7
> 3
> 4
>
> How can i find the number of match data? I try to do some elaboration. i take the first value '2' and compare against the rest of the data to find a match. If yes, counter + 1. Then i take the next value '3' and do the comparison again.
>
> Expected result:
>
> THere are 3 sets of match data, 2, 3 and 4.
>
> I could not achieve the results using the unique function, because it reduces to something like
> 2
> 3
> 4
> 5
> 6
> 7
>
> I tried using ismember too, but the code is not so dynamic, since i need to makes several changes when i change the input data.
>
> Is there any way where i can achieve my expected results? Thanks for helping.

help UNIQUE

Bruno

Subject: Finding the number of match data

From: us

Date: 24 Mar, 2009 12:27:01

Message: 3 of 4

"Lin "
> Say I have something as shown:
> x' =
> 2
> How can i find the number of match data? I try to do some elaboration. i take the first value '2' and compare against the rest of the data to find a match. If yes, counter + 1. Then i take the next value '3' and do the comparison again.
> I tried using ismember too, but the code is not so dynamic, since i need to makes several changes when i change the input data...

a hint:

     help histc;
% eg,
     d=[2 3 4 5 2 6 7 3 4];
     tmpl=1:7;
     n=histc(d,tmpl);
     disp([tmpl;n]);
%{
     1 2 3 4 5 6 7 % <- tmpl
     0 2 2 2 1 1 1 % <- # ocurrencies
%}

us

Subject: Finding the number of match data

From: Jos

Date: 24 Mar, 2009 14:55:03

Message: 4 of 4

"Lin " <shenqlv@yahoo.com> wrote in message <gqai9h$66b$1@fred.mathworks.com>...
> Hi all.
>
> Say I have something as shown:
>
> x' =
> 2
> 3
> 4
> 5
> 2
> 6
> 7
> 3
> 4
>
> How can i find the number of match data? I try to do some elaboration. i take the first value '2' and compare against the rest of the data to find a match. If yes, counter + 1. Then i take the next value '3' and do the comparison again.
>
> Expected result:
>
> THere are 3 sets of match data, 2, 3 and 4.
>
> I could not achieve the results using the unique function, because it reduces to something like
> 2
> 3
> 4
> 5
> 6
> 7
>
> I tried using ismember too, but the code is not so dynamic, since i need to makes several changes when i change the input data.
>
> Is there any way where i can achieve my expected results? Thanks for helping.

The general approach involves both UNIQUE and HISTC

histc(X,unique(X))

For positive integers, one could use the sparse trick:

x = [2 3 4 3 5 100 2 3 4 3 4] ;
[Val,N,N] = find(sparse(x,1,1)) ; % 2nd output unused
disp([Val N]

hth
Jos

Tags for 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