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 occurences of unique value in a matrix

Subject: Counting occurences of unique value in a matrix

From: Rahul

Date: 28 Jun, 2011 13:47:20

Message: 1 of 2

Hi,

My current task is to take a 2-D matrix as input, find all the unique values in the matrix and the number of instances each value is repeated in the matrix.

I am currently implementing this in the following manner:

values = unique(x);
instances = histc(x,values);

However, the matrix x (e.g. size 154 X 2048) and the vector x (e.g. length 157697) are both very large and histc runs out of memory in this case. Can someone suggest a more efficient way of achieving this.

Thanks

Subject: Counting occurences of unique value in a matrix

From: Roger Stafford

Date: 28 Jun, 2011 19:26:05

Message: 2 of 2

"Rahul " <rahul@segfault.co.in> wrote in message <iucm17$cmc$1@newscl01ah.mathworks.com>...
> Hi,
>
> My current task is to take a 2-D matrix as input, find all the unique values in the matrix and the number of instances each value is repeated in the matrix.
>
> I am currently implementing this in the following manner:
>
> values = unique(x);
> instances = histc(x,values);
>
> However, the matrix x (e.g. size 154 X 2048) and the vector x (e.g. length 157697) are both very large and histc runs out of memory in this case. Can someone suggest a more efficient way of achieving this.
>
> Thanks
- - - - - - - - - - -
  Are you sure this is what you want? Here is a sentence out of the 'histc' documentation: "For matrices, histc(x,edges) returns a matrix of column histogram counts." This means that 'instances' will have as many rows as there are unique values in all of x and it will have 2048 columns, each one a histogram. If all the values in x happened to be different that would give 'instances' a total of over six hundred million elements. This is undoubtedly why you are having memory problems.

  Maybe you want this:

 values = unique(x);
 instances = histc(x(:),values);

which will give you only one column of histogram counts.

  Here is an alternate method to try. For your size array it could prove faster even though the code takes four lines. (Can't hurt to try.)

 y = sort(x(:));
 p = find([true;diff(y)~=0;true]);
 values = y(p(1:end-1));
 instances = diff(p);

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