Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Changing negative sign

Subject: Changing negative sign

From: Jonathan

Date: 23 Mar, 2010 17:14:22

Message: 1 of 4

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

Subject: Changing negative sign

From: Steven Lord

Date: 23 Mar, 2010 17:26:31

Message: 2 of 4


"Jonathan" <jkakiwi@yahoo.co.uk> wrote in message
news:hoaste$1cs$1@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@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Changing negative sign

From: Jonathan

Date: 23 Mar, 2010 17:38:23

Message: 3 of 4

ABS works!.

Thank you,
Jon

"Steven Lord" <slord@mathworks.com> wrote in message <hoatjs$dfd$1@fred.mathworks.com>...
>
> "Jonathan" <jkakiwi@yahoo.co.uk> wrote in message
> news:hoaste$1cs$1@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@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Subject: Changing negative sign

From: dpb

Date: 23 Mar, 2010 18:16:00

Message: 4 of 4

Jonathan wrote:
> ABS works!.
...

DOH... :)

--

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us