From: Jonathan on
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

"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
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
Jonathan wrote:
> ABS works!.
....

DOH... :)

--