Prev: Jordan m file
Next: Real Time Workshop build error 1070
From: Jonathan on 23 Mar 2010 13:14 G'day, I have the following dataset... A = [-2 -2 -2 -2 -10 -2 10 26 18 6 ...] What I would like to do is change the negative values to positive values and leave the positive values unchanged. I have the following code... [r] = find(A<0) if A<0 test = -(A(r)); else test = A; end However, this does not do anything. What am I missing? Thanks Jon
From: Steven Lord on 23 Mar 2010 13:26 "Jonathan" <jkakiwi(a)yahoo.co.uk> wrote in message news:hoaste$1cs$1(a)fred.mathworks.com... > G'day, > > I have the following dataset... > > A = [-2 -2 -2 -2 -10 -2 10 26 18 6 ...] > > What I would like to do is change the negative values to positive values > and leave the positive values unchanged. > > I have the following code... > > [r] = find(A<0) > if A<0 > test = -(A(r)); > else > test = A; > end > > However, this does not do anything. Sure it does. It changes the value of the test temporary variable. It may not change the _contents of A_ but you didn't tell it to do so. If you want the absolute value of the elements of A, why not just use the ABSolute value function? BTW, "if A < 0" may not do what you expect -- see the documentation for IF for an explanation of how it treats nonscalar expressions. -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
From: Jonathan on 23 Mar 2010 13:38 ABS works!. Thank you, Jon "Steven Lord" <slord(a)mathworks.com> wrote in message <hoatjs$dfd$1(a)fred.mathworks.com>... > > "Jonathan" <jkakiwi(a)yahoo.co.uk> wrote in message > news:hoaste$1cs$1(a)fred.mathworks.com... > > G'day, > > > > I have the following dataset... > > > > A = [-2 -2 -2 -2 -10 -2 10 26 18 6 ...] > > > > What I would like to do is change the negative values to positive values > > and leave the positive values unchanged. > > > > I have the following code... > > > > [r] = find(A<0) > > if A<0 > > test = -(A(r)); > > else > > test = A; > > end > > > > However, this does not do anything. > > Sure it does. It changes the value of the test temporary variable. It may > not change the _contents of A_ but you didn't tell it to do so. > > If you want the absolute value of the elements of A, why not just use the > ABSolute value function? > > BTW, "if A < 0" may not do what you expect -- see the documentation for IF > for an explanation of how it treats nonscalar expressions. > > -- > Steve Lord > slord(a)mathworks.com > comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ >
From: dpb on 23 Mar 2010 14:16 Jonathan wrote: > ABS works!. .... DOH... :) --
|
Pages: 1 Prev: Jordan m file Next: Real Time Workshop build error 1070 |