Prev: Filtering out NaN
Next: About the Modeling
From: Dan Thatcher on 4 Jul 2010 06:53 Hi everyone, How can I process a matrix in an 'if' statement? I have a matrix of data either side of zero, for all positive values I want to apply a polynomial equation, all negative values, a different equation. For example: g=[100 100 100 100]; %polynomial 1 n=[1 1 1 1]; %polynomial 2 d=[1 -4 5 -7 4 -3 56 87]; if d>0 y=polyval(n,d) elseif d<0 y=polyval(g,d) end I can only get this to work if 'd' contains 1 value. Any suggestions?
From: us on 4 Jul 2010 07:02 "Dan Thatcher" <d.thatcher85(a)gmail.com> wrote in message <i0pp6h$bf0$1(a)fred.mathworks.com>... > Hi everyone, > > How can I process a matrix in an 'if' statement? I have a matrix of data either side of zero, for all positive values I want to apply a polynomial equation, all negative values, a different equation. For example: > > g=[100 100 100 100]; %polynomial 1 > n=[1 1 1 1]; %polynomial 2 > d=[1 -4 5 -7 4 -3 56 87]; > > if d>0 > y=polyval(n,d) > elseif d<0 > y=polyval(g,d) > end > > I can only get this to work if 'd' contains 1 value. Any suggestions? one of the many solutions - you do not need an IF statement... d=[1 -4 5 -7 4 -3 56 87]; dpos=d(d>0); ypos=polyval(n,dpos); % same for neg values in D... us
From: Dan Thatcher on 4 Jul 2010 08:39 > one of the many solutions > - you do not need an IF statement... > > d=[1 -4 5 -7 4 -3 56 87]; > dpos=d(d>0); > ypos=polyval(n,dpos); > % same for neg values in D... > > us Hi us, Thanks for the reply, the solution is much simpler than I expected! How can I combine the answers as a matrix the same size as the input ('d')? So the answer would be a matrix: [ypos yneg ypos. . .ypos ypos]
From: us on 4 Jul 2010 09:04 "Dan Thatcher" <d.thatcher85(a)gmail.com> wrote in message <i0pvdp$i3r$1(a)fred.mathworks.com>... > > one of the many solutions > > - you do not need an IF statement... > > > > d=[1 -4 5 -7 4 -3 56 87]; > > dpos=d(d>0); > > ypos=polyval(n,dpos); > > % same for neg values in D... > > > > us > > Hi us, > Thanks for the reply, the solution is much simpler than I expected! How can I combine the answers as a matrix the same size as the input ('d')? So the answer would be a matrix: > [ypos yneg ypos. . .ypos ypos] one of the solutions - note: extended use of logical indexing... d=[1 -4 5 -7 4 -3 56 87]; ix=d>0; d(ix)=polyval(n,d(ix)); % <- re-use D to save memory... % now, for neg vals... ix=d<0; us
|
Pages: 1 Prev: Filtering out NaN Next: About the Modeling |