From: Namo Namo on 16 Jul 2010 14:17 Say I have a matrix a = [ 1 2 2 2 3 3 1 4 5 ]; I want to count the occurences of 1 2 ... 5, but by how many rows. That is, although 2 appears 3 times, but only in 2 rows. I can count the total times by tabulate(a(:)) or histc, accumarray, etc. But to count the total rows, I am doing a for loop for i = 1:N count(i) = sum( any( a==i, 2) ) end any advice on how to speep up for large N? Thanks.
From: us on 16 Jul 2010 14:45 "Namo Namo" <wynamo(a)yahoo.com> wrote in message <i1q7ng$78g$1(a)fred.mathworks.com>... > Say I have a matrix > > a = [ 1 2 2 > 2 3 3 > 1 4 5 ]; > > I want to count the occurences of 1 2 ... 5, but by how many rows. That is, although 2 appears 3 times, but only in 2 rows. I can count the total times by tabulate(a(:)) or histc, accumarray, etc. But to count the total rows, I am doing a for loop > > for i = 1:N > count(i) = sum( any( a==i, 2) ) > end > > any advice on how to speep up for large N? Thanks. one of the solutions % the data a=[ 1 2 2 2 3 3 1 4 5 ]; % the engine nx=1:max(a(:)); n=histc(a.',nx); n=sum(n~=0,2); % the result disp([nx;n.']); %{ 1 2 3 4 5 % <= unique val... 2 2 1 1 1 % <- #rows %} us
From: Sean on 16 Jul 2010 15:09 "Namo Namo" <wynamo(a)yahoo.com> wrote in message <i1q7ng$78g$1(a)fred.mathworks.com>... > Say I have a matrix > > a = [ 1 2 2 > 2 3 3 > 1 4 5 ]; > > I want to count the occurences of 1 2 ... 5, but by how many rows. That is, although 2 appears 3 times, but only in 2 rows. I can count the total times by tabulate(a(:)) or histc, accumarray, etc. But to count the total rows, I am doing a for loop > > for i = 1:N > count(i) = sum( any( a==i, 2) ) > end > > any advice on how to speep up for large N? Thanks. Another solution: A = [2 3 2; 4 2 5; 1 3 7]; Acell = cellfun(@unique,mat2cell(A,ones(1,3),3),'UniformOutput',false); U = unique(A); [n] = histc(cell2mat(Acell'),U); table = [U, n'] %U is the value n is the occurance
From: Namo Namo on 16 Jul 2010 15:13 > > Say I have a matrix > > > > a = [ 1 2 2 > > 2 3 3 > > 1 4 5 ]; > > > > I want to count the occurences of 1 2 ... 5, but by how many rows. That is, although 2 appears 3 times, but only in 2 rows. I can count the total times by tabulate(a(:)) or histc, accumarray, etc. But to count the total rows, I am doing a for loop > > > > for i = 1:N > > count(i) = sum( any( a==i, 2) ) > > end > > > > any advice on how to speep up for large N? Thanks. > > one of the solutions > > % the data > a=[ > 1 2 2 > 2 3 3 > 1 4 5 > ]; > % the engine > nx=1:max(a(:)); > n=histc(a.',nx); > n=sum(n~=0,2); > % the result > disp([nx;n.']); > %{ > 1 2 3 4 5 % <= unique val... > 2 2 1 1 1 % <- #rows > %} > > us I actually used this before. It becomes slow for me because for me, a is like 1e5 rows and 5 columns, and max(a(:)) = 4000. So it takes a long time and generates a large n when I perform histc for each row of a, each time sorting 5 numbers into 4000 bins :-(. Thanks anyway.
From: Bruno Luong on 16 Jul 2010 15:18 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
|
Next
|
Last
Pages: 1 2 3 Prev: Problem with deployment Next: reassigning values of elements in matrix |