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:
how many times a digit is present in an array

Subject: how many times a digit is present in an array

From: muzaffar

Date: 1 Aug, 2008 15:34:02

Message: 1 of 12

Hello all,
how i can simply calculate number of repeating values.
A=[1 1 1 2 2 2 2 3 3 3 3 5 5 5 8 8 8 8 11 11 11 12 12 ];
how i can find out ?
number of 1's = 3
number of 2's = 4
number of 3's = 4
number of 5's = 3
number of 8's = 4
number of 11's = 3
number of 12's = 2
A is always sorted as above.

Subject: how many times a digit is present in an array

From: Ganesh

Date: 1 Aug, 2008 15:39:56

Message: 2 of 12

I cannot send you the code as my matlab is tied up.
Define B=3Dunique(A) to get each unique number from A and in order to
find the number of repetitions of each member of B in the array A, use
find command like : n(i)=3Dsize(find(A(:)=3D=3DB(i)));
Syntax maynot be correct, as i said my computer is tied in another
job, try and play around this.
-Ganesh

On Aug 1, 11:34=A0am, "muzaffar " <muzaffarbas...@yahoo.com> wrote:
> Hello all,
> how i can simply calculate number of repeating values.
> A=3D[1 1 1 2 2 2 2 3 3 3 3 5 5 5 =A08 8 8 8 =A011 11 11 12 12 ];
> how =A0i can find out ?
> number of 1's =3D 3
> number of 2's =3D 4
> number of 3's =3D 4
> number of 5's =3D 3
> number of 8's =3D 4
> number of 11's =3D 3
> number of 12's =3D 2
> A is always sorted as above.

Subject: how many times a digit is present in an array

From: D Stobbe

Date: 1 Aug, 2008 15:41:02

Message: 3 of 12

"muzaffar " <muzaffarbashir@yahoo.com> wrote in message

one way is to use the find function;

ie. if you want to find out how many times x appears in y.
(x is scalar and y is array)

numx = length(find(y==x,length(y)))

*****
find(argument to find , max number of indicies to return,
using the length y will insure every case is returned)

Subject: how many times a digit is present in an array

From: Donn Shull

Date: 1 Aug, 2008 15:43:03

Message: 4 of 12

"muzaffar " <muzaffarbashir@yahoo.com> wrote in message
<g6vada$92c$1@fred.mathworks.com>...
> Hello all,
> how i can simply calculate number of repeating values.
> A=[1 1 1 2 2 2 2 3 3 3 3 5 5 5 8 8 8 8 11 11 11 12 12 ];
> how i can find out ?
> number of 1's = 3
> number of 2's = 4
> number of 3's = 4
> number of 5's = 3
> number of 8's = 4
> number of 11's = 3
> number of 12's = 2
> A is always sorted as above.
>
>
>
>

sum(A == 1)

etc

Subject: how many times a digit is present in an array

From: D Stobbe

Date: 1 Aug, 2008 15:44:02

Message: 5 of 12

hmm, i don't know if my last reply when through...

to find how many x (scalar) in y (vector) use..

numx = length(find(y==x,length(y)))

Subject: how many times a digit is present in an array

From: utab

Date: 1 Aug, 2008 16:00:41

Message: 6 of 12

On Fri, 01 Aug 2008 15:34:02 +0000, muzaffar wrote:

> Hello all,
> how i can simply calculate number of repeating values. A=[1 1 1 2 2 2 2
> 3 3 3 3 5 5 5 8 8 8 8 11 11 11 12 12 ]; how i can find out ?
> number of 1's = 3
> number of 2's = 4
> number of 3's = 4
> number of 5's = 3
> number of 8's = 4
> number of 11's = 3
> number of 12's = 2
> A is always sorted as above.

See if this helps a bit I did not test much though

A=[1 1 1 2 2 2 2 3 3 3 3 5 5 5 8 8 8 8 11 11 11 12 12 ];
[A_u,i,j] = unique(A,'first');
% add the last index of the element array
counts = [diff(i) length(i(end):length(A))]
...
counts =

     3 4 4 3 4 3 2

Subject: how many times a digit is present in an array

From: D Stobbe

Date: 1 Aug, 2008 16:18:02

Message: 7 of 12

"muzaffar " <muzaffarbashir@yahoo.com> wrote in message


b=unique(A);
for i=1:length(b);
num_each(i,1)=b(i);
num_each(i,2)=sum(A==b(i));
end

num_each = [number,howmany time]

Subject: how many times a digit is present in an array

From: Aaron Callard

Date: 1 Aug, 2008 16:37:02

Message: 8 of 12

diff([0 find(diff(A)) length(A)])
is short and sweet

Subject: how many times a digit is present in an array

From: Rune Allnor

Date: 1 Aug, 2008 16:55:03

Message: 9 of 12

On 1 Aug, 17:34, "muzaffar " <muzaffarbas...@yahoo.com> wrote:
> Hello all,
> how i can simply calculate number of repeating values.
> A=3D[1 1 1 2 2 2 2 3 3 3 3 5 5 5 =A08 8 8 8 =A011 11 11 12 12 ];
> how =A0i can find out ?
> number of 1's =3D 3
> number of 2's =3D 4
> number of 3's =3D 4
> number of 5's =3D 3
> number of 8's =3D 4
> number of 11's =3D 3
> number of 12's =3D 2
> A is always sorted as above.

What's wrong with the good old histogram?

Rune

Subject: how many times a digit is present in an array

From: Ilya Rozenfeld

Date: 1 Aug, 2008 16:56:03

Message: 10 of 12

N = hist(x, unique(x))


"muzaffar " <muzaffarbashir@yahoo.com> wrote in message
<g6vada$92c$1@fred.mathworks.com>...
> Hello all,
> how i can simply calculate number of repeating values.
> A=[1 1 1 2 2 2 2 3 3 3 3 5 5 5 8 8 8 8 11 11 11 12
12 ];
> how i can find out ?
> number of 1's = 3
> number of 2's = 4
> number of 3's = 4
> number of 5's = 3
> number of 8's = 4
> number of 11's = 3
> number of 12's = 2
> A is always sorted as above.
>
>
>
>

Subject: how many times a digit is present in an array

From: Bruno Luong

Date: 1 Aug, 2008 17:25:04

Message: 11 of 12

"Ilya Rozenfeld" <rozeni.nospam@alum.rpi.edu> wrote in
message <g6vf73$4d3$1@fred.mathworks.com>...
> N = hist(x, unique(x))

Very good!

Bruno

Subject: how many times a digit is present in an array

From: muzaffar

Date: 4 Aug, 2008 12:57:01

Message: 12 of 12

"Ilya Rozenfeld" <rozeni.nospam@alum.rpi.edu> wrote in
message <g6vf73$4d3$1@fred.mathworks.com>...
> N = hist(x, unique(x))
>
>
> "muzaffar " <muzaffarbashir@yahoo.com> wrote in message
> <g6vada$92c$1@fred.mathworks.com>...
> > Hello all,
> > how i can simply calculate number of repeating values.
> > A=[1 1 1 2 2 2 2 3 3 3 3 5 5 5 8 8 8 8 11 11 11 12
> 12 ];
> > how i can find out ?
> > number of 1's = 3
> > number of 2's = 4
> > number of 3's = 4
> > number of 5's = 3
> > number of 8's = 4
> > number of 11's = 3
> > number of 12's = 2
> > A is always sorted as above.
> >
> >
> >
> >
>
Thanks you Ilya Rozenfeld and all others,
the problem is solved as the following:
N = hist(x, unique(x));
xvalues=unique(x);

xvalues =
1 2 3 5 8 11 12

N =
3 4 4 3 4 3 2

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