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:
[Q][HURRY!] Counting Cardinality

Subject: [Q][HURRY!] Counting Cardinality

From: Lee Jiwoong

Date: 29 Apr, 1999 03:28:46

Message: 1 of 7

Dear Seniors. This is an urgent problem.

I need to count the occurence frequency-cardinality-of each element of a
vector.

For example, if X = [ 1 5 2 1 1 5 1], then, I need the result like this:
                      Y = [4 1 0 0 2]

since 1 appears 4 times, two appears 1 times, 3 and 4 appears zero times and
five
appears two times.

That is, I need to count the occurence frequency.

What I found as a temporary solution is using for loops like :

        x=[1 3 0 1 9 12 0 ...... 1 3];
        f=[1:max(x1c)].*0;
        for I=1:length(x1c),
            f(x(I))=f(x(I))+1;
        end
        f

Naturally this approace quite degrades the system performance - extreamly
low speed.

Anyone who can suggest the most proper solution to this ?

Thanks .

Jiwoong Lee
porce@kaist.ac.kr

Subject: [Q][HURRY!] Counting Cardinality

From: Dines Justesen

Date: 28 Apr, 1999 14:42:40

Message: 2 of 7

Try y=hist(X,max(X))

Dines

BTW tabulate also does this, but it also returns other info

Lee Jiwoong <porce@kaist.ac.kr> wrote in message
news:7g7k1b$i2i$1@news.kren.nm.kr...
> Dear Seniors. This is an urgent problem.
>
> I need to count the occurence frequency-cardinality-of each element of a
> vector.
>
> For example, if X = [ 1 5 2 1 1 5 1], then, I need the result like this:
> Y = [4 1 0 0 2]
>
> since 1 appears 4 times, two appears 1 times, 3 and 4 appears zero times
and
> five
> appears two times.
>
> That is, I need to count the occurence frequency.
>
> What I found as a temporary solution is using for loops like :
>
> x=[1 3 0 1 9 12 0 ...... 1 3];
> f=[1:max(x1c)].*0;
> for I=1:length(x1c),
> f(x(I))=f(x(I))+1;
> end
> f
>
> Naturally this approace quite degrades the system performance - extreamly
> low speed.
>
> Anyone who can suggest the most proper solution to this ?
>
> Thanks .
>
> Jiwoong Lee
> porce@kaist.ac.kr
>
>

Subject: [Q][HURRY!] Counting Cardinality

From: Stefan Stoll

Date: 28 Apr, 1999 20:48:58

Message: 3 of 7

Lee Jiwoong wrote:
>
> Dear Seniors. This is an urgent problem.
>
> I need to count the occurence frequency-cardinality-of each element of a
> vector.
>
> For example, if X = [ 1 5 2 1 1 5 1], then, I need the result like this:
> Y = [4 1 0 0 2]
>
> since 1 appears 4 times, two appears 1 times, 3 and 4 appears zero times and
> five
> appears two times.
>
> That is, I need to count the occurence frequency.
>
> What I found as a temporary solution is using for loops like :
>
> x=[1 3 0 1 9 12 0 ...... 1 3];
> f=[1:max(x1c)].*0;
> for I=1:length(x1c),
> f(x(I))=f(x(I))+1;
> end
> f
>
> Naturally this approace quite degrades the system performance - extreamly
> low speed.
>
> Anyone who can suggest the most proper solution to this ?
>
> Thanks .
>
> Jiwoong Lee
> porce@kaist.ac.kr

Possible solution:

  for i=1:length(X), Y(i)=length(find(X==i)); end;

sTefan

Subject: [Q][HURRY!] Counting Cardinality

From: Ken Davis

Date: 28 Apr, 1999 15:57:49

Message: 4 of 7

I think that diff([0;(find(diff(sort(X(:)))));length(X)]) should give you
the frequency of occurrence of elements which occur at least once. These
elements can be found with the function unique(X). Integrating the 0's for
the elements that do not occur is left as an exercise for the reader. - Ken


Lee Jiwoong wrote in message <7g7k1b$i2i$1@news.kren.nm.kr>...
>Dear Seniors. This is an urgent problem.
>
>I need to count the occurence frequency-cardinality-of each element of a
>vector.
>
>For example, if X = [ 1 5 2 1 1 5 1], then, I need the result like this:
> Y = [4 1 0 0 2]
>
>since 1 appears 4 times, two appears 1 times, 3 and 4 appears zero times
and
>five
>appears two times.
>
>That is, I need to count the occurence frequency.
>
>What I found as a temporary solution is using for loops like :
>
> x=[1 3 0 1 9 12 0 ...... 1 3];
> f=[1:max(x1c)].*0;
> for I=1:length(x1c),
> f(x(I))=f(x(I))+1;
> end
> f
>
>Naturally this approace quite degrades the system performance - extreamly
>low speed.
>
>Anyone who can suggest the most proper solution to this ?
>
>Thanks .
>
>Jiwoong Lee
>porce@kaist.ac.kr
>
>

Subject: [Q][HURRY!] Counting Cardinality

From: Carsten Fritsch

Date: 29 Apr, 1999 10:04:05

Message: 5 of 7

There is another history function called 'histc' in Matlab, which will not open
a figure window:

use X = [ 1 5 2 1 1 5 1]

                Y=histc(X,min(X):max(X)) or
                Y=histc(X,1:max(X)) .

More Info use : 'help histc' in Matlab-Window.

Carsten Fritsch

Subject: [Q][HURRY!] Counting Cardinality

From: juhas@red.seas.upenn.edu (PAVOL JUHAS)

Date: 29 Apr, 1999 13:53:29

Message: 6 of 7

Lee Jiwoong (porce@kaist.ac.kr) wrote:
: Dear Seniors. This is an urgent problem.

: I need to count the occurence frequency-cardinality-of each element of a
: vector.

: For example, if X = [ 1 5 2 1 1 5 1], then, I need the result like this:
: Y = [4 1 0 0 2]

: since 1 appears 4 times, two appears 1 times, 3 and 4 appears zero times and
: five
: appears two times.

: That is, I need to count the occurence frequency.

A fast solution is:

Y=full( sparse(1,X,1,1,max(X)) );

Pavol

Subject: [Q][HURRY!] Counting Cardinality

From: David Goodmanson

Date: 4 May, 1999 12:54:30

Message: 7 of 7

PAVOL JUHAS wrote:
>
> A fast solution is:
>
> Y=full( sparse(1,X,1,1,max(X)) );
>
> Pavol

Unfortunately, this only *used* to be fast. Matlab 4 SPARSE was fine in
this situation, but in 5.3 and 5.2 (I don't know about 5.1 and 5.0)
SPARSE can be extremely slow. Matlab 5.3 SPARSE sorts vector X in a
time proportional to length(X)^2, so if X has lots of elements you're in
trouble. (MathWorks proudly introduces ... bubble sort!?) Whatever,
the workaround is to presort X:
sparse(1,sort(X),1,1,max(X));
MathWorks has acknowledged the problem, and they have even released a
timetable for the bug fix. It's going to happen "in a future version of
MATLAB". ;-)

--Dave

*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

David Goodmanson dgoodmanson@worldnet.att.net
909 4th Ave. N, Apt. D
Seattle, WA 98109 USA

He had bought a large map representing the sea,
  Without the least vestige of land:
And the crew were much pleased when they found it to be
  A map they could all understand.

--Lewis Carroll

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