From: us on
"Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i1qb9c$oo4$1(a)fred.mathworks.com>...
> Another solution:
>
> a=[1 2 2;
> 2 3 3;
> 1 4 5];
>
> sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
>
> Bruno

the problem with all these nice solutions
- given the OP's mat size...

a=ceil(4000*rand(1e5,5));
sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
%{
??? Error using ==> accumarray
Out of memory. Type HELP MEMORY for your options.
%}
% same with the HISTC approach...

us
From: Namo Namo on
Indeed. Sorry I should have made my question more clear.

I know there is a tradeoff between time and memory. Vectorized code often requires more use of memory to hold indices. I guess I will just go with for loop for now. Thanks for the all the replies!



"us " <us(a)neurol.unizh.ch> wrote in message <i1qcj8$idl$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i1qb9c$oo4$1(a)fred.mathworks.com>...
> > Another solution:
> >
> > a=[1 2 2;
> > 2 3 3;
> > 1 4 5];
> >
> > sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
> >
> > Bruno
>
> the problem with all these nice solutions
> - given the OP's mat size...
>
> a=ceil(4000*rand(1e5,5));
> sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
> %{
> ??? Error using ==> accumarray
> Out of memory. Type HELP MEMORY for your options.
> %}
> % same with the HISTC approach...
>
> us
From: Bruno Luong on
"us " <us(a)neurol.unizh.ch> wrote in message <i1qcj8$idl$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i1qb9c$oo4$1(a)fred.mathworks.com>...
> > Another solution:
> >
> > a=[1 2 2;
> > 2 3 3;
> > 1 4 5];
> >
> > sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
> >
> > Bruno
>
> the problem with all these nice solutions
> - given the OP's mat size...
>
> a=ceil(4000*rand(1e5,5));
> sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
> %{
> ??? Error using ==> accumarray
> Out of memory. Type HELP MEMORY for your options.
> %}
> % same with the HISTC approach...

Well, OP can break the matrix to smaller pieces, no need to eat at once an elephant.

Bruno
From: Sean on
"us " <us(a)neurol.unizh.ch> wrote in message <i1qcj8$idl$1(a)fred.mathworks.com>...
> "Bruno Luong" <b.luong(a)fogale.findmycountry> wrote in message <i1qb9c$oo4$1(a)fred.mathworks.com>...
> > Another solution:
> >
> > a=[1 2 2;
> > 2 3 3;
> > 1 4 5];
> >
> > sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
> >
> > Bruno
>
> the problem with all these nice solutions
> - given the OP's mat size...
>
> a=ceil(4000*rand(1e5,5));
> sum(accumarray([mod((0:numel(a)-1)',size(a,1))+1 a(:)],1)>0)
> %{
> ??? Error using ==> accumarray
> Out of memory. Type HELP MEMORY for your options.
> %}
> % same with the HISTC approach...
>
> us

tic
A=ceil(4000*rand(1e5,5));
Acell = cellfun(@unique,mat2cell(A,ones(1,size(A,1)),size(A,2)),'UniformOutput',false);
U = unique(A);
[n] = histc(cell2mat(Acell'),U);
table = [U, n']; %U is the value n is the occurance
toc
%Elapsed time is 4.247591 seconds.
%table = 4000 x 2 double
From: Bruno Luong on
Almost the same solution, but for large size:

a=ceil(4000*rand(1e5,5));

full(sum( sparse(mod((0:numel(a)-1)',size(a,1))+1, a(:), 1) > 0,1))

% Bruno