hello,
I wonder if someone can help. I've got data that is constantly updated being imported into matlab at the moment blank spaces for missing data are being filled with zeros I want to calculate the mean etc.. for these rows but do not want to lose the zeros sandwiched in the data by using things like a(a==0))=NaN followed by nanmean. Is there a way to change zeros to Nan on either end of the row but not the zeros sandwiched inbetween the data?
Thanks in advance
us - 11 Jul 2008 11:58 GMT
Jess:
<SNIP bad tasting sandwich...
one of the solutions
v=[0,0,0,1,0,2,0,0,3,0,0];
v(1:find(v~=0,1,'first')-1)=nan;
v(find(v~=0,1,'last')+1:end)=nan;
disp(v)
% v = NaN NaN NaN 1 0 2 0 0 3 NaN NaN
us
Jess - 11 Jul 2008 12:19 GMT
Jos - 11 Jul 2008 13:27 GMT
Jess <jesslaffoley89@hotmail.com> wrote in message
<23774766.1215773111872.JavaMail.jakarta@nitrogen.mathforum.org>...
> hello,
>
> I wonder if someone can help. I've got data that is constantly updated being imported into matlab at the moment
blank spaces for missing data are being filled with zeros I
want to calculate the mean etc.. for these rows but do not
want to lose the zeros sandwiched in the data by using
things like a(a==0))=NaN followed by nanmean. Is there a
way to change zeros to Nan on either end of the row but not
the zeros sandwiched inbetween the data?
> Thanks in advance
US provided you with the solution to transform you original
vector, so that you can use nanmean. I want to suggest that
you also could use mean on the portion of the array you're
interested in:
V = [0 0 0 1 2 0 3 0 4 0 0 0 0 0] ;
W = V(find(V~=0,1,'first'):find(V~=0,1,'last')) ;
mean(W)
% or directly mean(V(find(..):find(..)))
which is faster than using nanmean
hth
Jos