"Rahul " <rahul@segfault.co.in> wrote in message <iucm17$cmc$1@newscl01ah.mathworks.com>...
> Hi,
>
> 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.
>
> 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:end1));
instances = diff(p);
Roger Stafford
