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:
Count how many times a value is present in a vector

Subject: Count how many times a value is present in a vector

From: Thijs

Date: 15 Jul, 2010 12:04:05

Message: 1 of 6

Hello,

I have a rowvector with a length of app. 300 that is filled with integer values ranging from 1 to app. 13000. Therefore, most values (out of the range of 1 to 13000) will not be present in the vector, some values will be unique, but other values are present multiple times. I am not really interested in the values that occur only once in the vector, but I do want to know which values occur more than once and also how many times these values occur.

In short, I have two questions:

- How can I check which values occur more than once in my vector?
- How can I see how many occurences (of the values out of my first question) there are?

Thanks in advance for the help.

Subject: Count how many times a value is present in a vector

From: Thijs

Date: 15 Jul, 2010 12:53:06

Message: 2 of 6

Ok, I have solved one of my two questions myself by:

x = length(v1)-length(unique(v1))

Now only one question remains, how can I find out which numbers were not unique in the rowvector v1?

Thanks.

Thijs

"Thijs " <t.denhamer@student.tudelft.nl> wrote in message <i1mtfl$73i$1@fred.mathworks.com>...
> Hello,
>
> I have a rowvector with a length of app. 300 that is filled with integer values ranging from 1 to app. 13000. Therefore, most values (out of the range of 1 to 13000) will not be present in the vector, some values will be unique, but other values are present multiple times. I am not really interested in the values that occur only once in the vector, but I do want to know which values occur more than once and also how many times these values occur.
>
> In short, I have two questions:
>
> - How can I check which values occur more than once in my vector?
> - How can I see how many occurences (of the values out of my first question) there are?
>
> Thanks in advance for the help.

Subject: Count how many times a value is present in a vector

From: Sean

Date: 15 Jul, 2010 13:10:11

Message: 3 of 6

"Thijs " <t.denhamer@student.tudelft.nl> wrote in message <i1n0bi$9cn$1@fred.mathworks.com>...
> Ok, I have solved one of my two questions myself by:
>
> x = length(v1)-length(unique(v1))
>
> Now only one question remains, how can I find out which numbers were not unique in the rowvector v1?
>
> Thanks.
>

Take a peak at the 2nd/3rd outputs from unique(). Function accumarray() will also help.

Subject: Count how many times a value is present in a vector

From: Sean

Date: 15 Jul, 2010 13:20:21

Message: 4 of 6

"Thijs " <t.denhamer@student.tudelft.nl> wrote in message <i1n0bi$9cn$1@fred.mathworks.com>...
> Ok, I have solved one of my two questions myself by:
>
> x = length(v1)-length(unique(v1))
>
> Now only one question remains, how can I find out which numbers were not unique in the rowvector v1?
>

You could do this all in one step with histc().

Subject: Count how many times a value is present in a vector

From: Matt J

Date: 15 Jul, 2010 13:27:33

Message: 5 of 6

"Thijs " <t.denhamer@student.tudelft.nl> wrote in message <i1mtfl$73i$1@fred.mathworks.com>...

> In short, I have two questions:
>
> - How can I check which values occur more than once in my vector?
> - How can I see how many occurences (of the values out of my first question) there are?
=============

Values=1:13000;
N=histc(YourVector,Values);

ValuesOccuringMoreThanOnce=Values(N>1);
NumberOfOccurences=N(N>1);

Subject: Count how many times a value is present in a vector

From: Thijs

Date: 15 Jul, 2010 13:40:12

Message: 6 of 6

Thanks everybody for your help. Especially Matt, your solution works perfectly!


"Matt J " <mattjacREMOVE@THISieee.spam> wrote in message <i1n2c5$jpn$1@fred.mathworks.com>...
> "Thijs " <t.denhamer@student.tudelft.nl> wrote in message <i1mtfl$73i$1@fred.mathworks.com>...
>
> > In short, I have two questions:
> >
> > - How can I check which values occur more than once in my vector?
> > - How can I see how many occurences (of the values out of my first question) there are?
> =============
>
> Values=1:13000;
> N=histc(YourVector,Values);
>
> ValuesOccuringMoreThanOnce=Values(N>1);
> NumberOfOccurences=N(N>1);

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