From: Bruno Luong on 14 Apr 2010 05:19 "us " <us(a)neurol.unizh.ch> wrote in message <hq40ng$e6i$1(a)fred.mathworks.com>... > "mb " <marit.berger(a)student.lu.se> wrote in message <hq3vlo$snc$1(a)fred.mathworks.com>... > > > > > > > > i don't see how you get from A to B given your task... > > > otherwise, > > > > > > one of the solutions > > > > > > m=[ > > > 1 0 0 1 > > > 1 1 0 0 > > > 1 1 0 0 > > > 1 0 0 1 > > > ]; > > > n=histc(sum(m),1:4) > > > % n = 0 2 0 1 > > > > > > us > > > > I don't want to take the sum of the columns, cause if it is a sequence of zeros between the ones I want to count the ones seperatly. So in matrix M I have 2 "sequences" with 1 ones, 1 sequence with 2 ones and 1 sequence with 4 ones. > > > > mb > > well... should have been said at the beginning... > anyhow, > > one of the many solutions > > m=[ > 1 0 0 1 > 1 1 0 0 > 1 1 0 0 > 1 0 0 1 > ]; > r=regexp(char([0,m(:).',0]+'0'),'0','split'); > n=histc(cellfun(@numel,r),1:4) > % n = 2 1 0 1 > The above won't work for m = ones(4) for example. Bruno
From: us on 14 Apr 2010 05:28 "Bruno Luong" > > r=regexp(char([0,m(:).',0]+'0'),'0','split'); > > n=histc(cellfun(@numel,r),1:4) > > % n = 2 1 0 1 > > > The above won't work for m = ones(4) for example. > Bruno well... let's stay calm, there's (almost) always a solution... :-) m=ones(4); m=[ zeros(1,size(m,2)); m ]; r=regexp(char([0,m(:).',0]+'0'),'0','split'); n=histc(cellfun(@numel,r),1:4) % n = 0 0 0 4 us
From: Sorry on 14 Apr 2010 06:00 %My solution %Counting sequences of ones %Initialize variables auxcounter=0; %We create the array m=[1 0 0 1;1 1 0 0;1 1 0 0;1 0 0 1]; %Getting the array height and width arraysize=size(m); %Initialize res array res=zeros(arraysize(1),1); %Now we are going to start counting, for every row: for i = 1:arraysize(1) %Restart counter auxcounter = 0; %For every column for j = 1:arraysize(2) %If i'm reading a zero if(m(i,j) == 0) if(auxcounter) %If there's something accumulated res(auxcounter)=res(auxcounter)+1; auxcounter=0; end else auxcounter=auxcounter+1; end end %If auxcounter then dump if(auxcounter) res(auxcounter)=res(auxcounter)+1; end end res'
From: Sorry on 14 Apr 2010 06:02 %My solution %Counting sequences of ones %Initialize variables auxcounter=0; %We create the array m=[1 0 0 1;1 1 0 0;1 1 0 0;1 0 0 1]; %Getting the array height and width arraysize=size(m); %Initialize res array res=zeros(arraysize(1),1); %Now we are going to start counting, for every row: for i = 1:arraysize(1) %Restart counter auxcounter = 0; %For every column for j = 1:arraysize(2) %If i'm reading a zero if(m(i,j) == 0) if(auxcounter) %If there's something accumulated res(auxcounter)=res(auxcounter)+1; auxcounter=0; end else auxcounter=auxcounter+1; end end %If auxcounter then dump if(auxcounter) res(auxcounter)=res(auxcounter)+1; end end res'
From: us on 14 Apr 2010 06:12 "Sorry " <fsolis(a)anafocus.com> wrote in message <hq43qs$du$1(a)fred.mathworks.com>... > %My solution > > %Counting sequences of ones > ...MANY LINES OF CODE... > res' wow... and, as we always say in this NG: one of the many solutions... unfortunately, did you check your RES(?)... res.' % 4 2 0 0 % <- ? us
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 Prev: How to make mcc function efficient Next: Hyperbolic geometry visualizations |