http://www.mathworks.com/matlabcentral/newsreader/view_thread/309839
MATLAB Central Newsreader  Counting occurences of unique value in a matrix
Feed for thread: Counting occurences of unique value in a matrix
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Tue, 28 Jun 2011 13:47:20 +0000
Counting occurences of unique value in a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309839#843220
Rahul
Hi,<br>
<br>
My current task is to take a 2D matrix as input, find all the unique values in the matrix and the number of instances each value is repeated in the matrix. <br>
<br>
I am currently implementing this in the following manner:<br>
<br>
values = unique(x);<br>
instances = histc(x,values);<br>
<br>
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.<br>
<br>
Thanks

Tue, 28 Jun 2011 19:26:05 +0000
Re: Counting occurences of unique value in a matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/309839#843292
Roger Stafford
"Rahul " <rahul@segfault.co.in> wrote in message <iucm17$cmc$1@newscl01ah.mathworks.com>...<br>
> Hi,<br>
> <br>
> My current task is to take a 2D matrix as input, find all the unique values in the matrix and the number of instances each value is repeated in the matrix. <br>
> <br>
> I am currently implementing this in the following manner:<br>
> <br>
> values = unique(x);<br>
> instances = histc(x,values);<br>
> <br>
> 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.<br>
> <br>
> Thanks<br>
          <br>
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.<br>
<br>
Maybe you want this:<br>
<br>
values = unique(x);<br>
instances = histc(x(:),values);<br>
<br>
which will give you only one column of histogram counts.<br>
<br>
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.)<br>
<br>
y = sort(x(:));<br>
p = find([true;diff(y)~=0;true]);<br>
values = y(p(1:end1));<br>
instances = diff(p);<br>
<br>
Roger Stafford