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:
Counting elements in a matrix

Subject: Counting elements in a matrix

From: Raghavasimhan Thirunarayanan

Date: 17 Feb, 2009 08:04:03

Message: 1 of 4

Hi all,
 I would like to know whether it is possible to list all the elements in an arbitrary matrix and the number of times they appear. For example if my matrix is
[ 1,2,3; 3,4,5; 5,6,7]
the program should list as follows
element no of appearances
    1 1
    2 1
    3 2
    4 1
    5 2
    6 1
    7 1.

I would much appreciate if anyone can help regarding this.

Thanks in advance

Subject: Counting elements in a matrix

From: Bruno Luong

Date: 17 Feb, 2009 08:12:03

Message: 2 of 4

"Raghavasimhan Thirunarayanan" <kingmakerbull@rediffmail.com> wrote in message <gndr1j$6th$1@fred.mathworks.com>...
> Hi all,
> I would like to know whether it is possible to list all the elements in an arbitrary matrix and the number of times they appear. For example if my matrix is
> [ 1,2,3; 3,4,5; 5,6,7]
> the program should list as follows
> element no of appearances
> 1 1
> 2 1
> 3 2
> 4 1
> 5 2
> 6 1
> 7 1.
>
> I would much appreciate if anyone can help regarding this.
>
> Thanks in advance

This problem comes now and then. See this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/239462#611824

Bruno

Subject: Counting elements in a matrix

From: wapo

Date: 17 Feb, 2009 08:12:30

Message: 3 of 4

On Feb 17, 9:04=A0am, "Raghavasimhan Thirunarayanan"
<kingmakerb...@rediffmail.com> wrote:
> Hi all,
> =A0I would like to know whether it is possible to list all the elements i=
n an arbitrary matrix and the number of times they appear. For example if m=
y matrix is
> [ 1,2,3; 3,4,5; 5,6,7]
> the program should list as follows
> element =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 no of appearances
> =A0 =A0 1 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A01
> =A0 =A0 2 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A01
> =A0 =A0 3 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A02
> =A0 =A0 4 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A01
> =A0 =A0 5 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A02
> =A0 =A0 6 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A01
> =A0 =A0 7 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0 =A0=
 =A01.
>
> I would much appreciate if anyone can help regarding this.
>
> Thanks in advance

If m is your matrix then hist(m(:))

Subject: Counting elements in a matrix

From: Roger Stafford

Date: 17 Feb, 2009 17:10:04

Message: 4 of 4

"Raghavasimhan Thirunarayanan" <kingmakerbull@rediffmail.com> wrote in message <gndr1j$6th$1@fred.mathworks.com>...
> ........
> I would like to know whether it is possible to list all the elements in an arbitrary matrix and the number of times they appear.
> ........

  Raghavasimhan, from Bruno's reference I call your attention to advice I gave there about a similar problem: "... use the third output argument of 'unique' in the 'accumarray' function rather than doing 'histc'. This has the advantage of requiring only one sort operation altogether."

Roger Stafford

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