Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
Mathematics
General TopicsResearchOperations ResearchStatisticsMathematical LogicNumerical AnalysisUndergraduate MathAlgebra HelpRecreational Math
Math Software
MapleMathematicaMATLABScilabSASSPSS

Math Forum / Math Software / MATLAB / July 2008



Tip: Looking for answers? Try searching our database.

losing zeros at the end and beginning of rows only

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jess - 11 Jul 2008 11:44 GMT
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
fantastic thank you
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
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2010 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.