From: Ulrik Nash on 29 Jun 2010 09:41 Hi everyone, Suppose I have A = [1 2 3 0 0 0; 0 3 0 5 0 0] What command lets me get B = [2;4] which is the mean of each row in A, disregarding zeros? Regards, Ulrik.
From: Adam Chapman on 29 Jun 2010 09:49 On Jun 29, 2:41 pm, "Ulrik Nash" <u...(a)sam.sdu.dk> wrote: > Hi everyone, > > Suppose I have > > A = [1 2 3 0 0 0; 0 3 0 5 0 0] > > What command lets me get > > B = [2;4] > > which is the mean of each row in A, disregarding zeros? > > Regards, > > Ulrik. I couldn't find a way to do all rows without a for loop, but here is a solution: for i=1:size(A,1) %get row data B=A(i,:); %remove zero values: B=B(B~=0); get mean of row: m(i,1)=mean(B); end
From: us on 29 Jun 2010 09:56 "Ulrik Nash" <uwn(a)sam.sdu.dk> wrote in message <i0ct5g$ekm$1(a)fred.mathworks.com>... > Hi everyone, > > Suppose I have > > A = [1 2 3 0 0 0; 0 3 0 5 0 0] > > What command lets me get > > B = [2;4] > > which is the mean of each row in A, disregarding zeros? > > Regards, > > Ulrik. one of the solutions - if(f) you own the stats tbx... % the data v=[ 1 2 3 0 0 0 0 3 0 5 0 0 ]; % the engine tf=v~=0; b=sum(tf,2); a=sum(v,2); r=a./b; % the result r % r = 2 4 us
From: Oleg Komarov on 29 Jun 2010 10:03 "Ulrik Nash" <uwn(a)sam.sdu.dk> wrote in message <i0ct5g$ekm$1(a)fred.mathworks.com>... > Hi everyone, > > Suppose I have > > A = [1 2 3 0 0 0; 0 3 0 5 0 0] > > What command lets me get > > B = [2;4] > > which is the mean of each row in A, disregarding zeros? > > Regards, > > Ulrik. % If you don't have the Stats toolbox [row,~,val] = find(A); accumarray(row,val,[],@mean) % If you have the Stats toolbox A(A == 0) = NaN; nanmean(A,2)
From: Matt Fig on 29 Jun 2010 10:07 "us " <us(a)neurol.unizh.ch> wrote in message > one of the solutions > - if(f) you own the stats tbx... > > % the data > v=[ > 1 2 3 0 0 0 > 0 3 0 5 0 0 > ]; > % the engine > tf=v~=0; > b=sum(tf,2); > a=sum(v,2); > r=a./b; > % the result > r > % r = 2 4 > > us Do I misread you us? Why does one need the stats toolbox for this? Isn't your solution the same as: rows_mean = sum(A,2)./sum(logical(A),2)
|
Next
|
Last
Pages: 1 2 Prev: Regarding reading data from excel sheet Next: make GUI callback function return a vaule |