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:
How do you represent "Not Equal To" ??

Subject: How do you represent "Not Equal To" ??

From: Arun Chhabra

Date: 30 Sep, 1998 14:09:55

Message: 1 of 4

*** To respond, remove "no-spam" from the reply-to address ***

How do you represent "Not Equal To" in Matlab?? Something like the
equivalent of "!=" in C.

I know that NOT is "~", however the combined expression of "~=" did not
work.

I'd appreciate any suggestions...I'm using Matlab 4.0.

Thanks in advance,
Arun

Subject: How do you represent "Not Equal To" ??

From: Matt Taylor

Date: 30 Sep, 1998 22:21:14

Message: 2 of 4

Arun,

It works for me, using ver 5.2. Example:

» y=[1 2 3 4 4 4 5 6]
y =
     1 2 3 4 4 4 5 6
» y~=4
ans =
     1 1 1 0 0 0 1 1

Matt Taylor


Arun Chhabra wrote in message <6uu6n7$s6i$1@dskd3.itg.ti.com>...
>*** To respond, remove "no-spam" from the reply-to address ***
>
>How do you represent "Not Equal To" in Matlab?? Something like the
>equivalent of "!=" in C.
>
>I know that NOT is "~", however the combined expression of "~=" did not
>work.
>
>I'd appreciate any suggestions...I'm using Matlab 4.0.
>
>Thanks in advance,
>Arun
>
>
>
>

Subject: How do you represent "Not Equal To" ??

From: Rob Henson

Date: 1 Oct, 1998 09:04:48

Message: 3 of 4

Hi Arun,

as Matt says, the not equal operator is "~=" . However in MATLAB it
is an element wise operator so in Matt's example notice that some
elements are 1s and others are 0s. This means that you must be
careful with this sort of thing

if a~=b
   do something
else
   do something else
end

As the if operator is true only if all elements of the clause are not zero.
So for Matt's example

>> y=[1 2 3 4 4 4 5 6];

This :

if y~=4
  foo = 1
else
  foo = 0
end

gives foo = 0, which may seem counter intuitive.


To get the "expected" behaviour you need to use the 'any' operator

if any(y~=4)
   foo = 1
else
  foo = 0
end

gives foo =1

For matrices rather than vectors, you will need any(any(Y~=4)).

I hope that helps.

Rob

Matt Taylor wrote:

> Arun,
>
> It works for me, using ver 5.2. Example:
>
> (I;(B y=[1 2 3 4 4 4 5 6]
> y =
> 1 2 3 4 4 4 5 6
> (I;(B y~=4
> ans =
> 1 1 1 0 0 0 1 1
>
> Matt Taylor
>
> Arun Chhabra wrote in message <6uu6n7$s6i$1@dskd3.itg.ti.com>...
> >*** To respond, remove "no-spam" from the reply-to address ***
> >
> >How do you represent "Not Equal To" in Matlab?? Something like the
> >equivalent of "!=" in C.
> >
> >I know that NOT is "~", however the combined expression of "~=" did not
> >work.
> >
> >I'd appreciate any suggestions...I'm using Matlab 4.0.
> >
> >Thanks in advance,
> >Arun
> >
> >
> >
> >



--
===========================================================
Robert Henson E-mail - rob@mathworks.com
The MathWorks, Inc. WWW - http://www.mathworks.com
24 Prime Parkway Phone - (508) 647-7505
Natick, MA 01760 FAX - (508) 647-7012
===========================================================

Subject: How do you represent "Not Equal To" ??

From: Ken Davis

Date: 1 Oct, 1998 11:49:46

Message: 4 of 4

for arbitrary ND array, you can use any(Y(:)~=4). - Ken


Rob Henson wrote in message <36137DF0.BAC52D32@mathworks.com>...
>Hi Arun,
>
>as Matt says, the not equal operator is "~=" . However in MATLAB it
>is an element wise operator so in Matt's example notice that some
>elements are 1s and others are 0s. This means that you must be
>careful with this sort of thing
>
>if a~=b
> do something
>else
> do something else
>end
>
>As the if operator is true only if all elements of the clause are not zero.
>So for Matt's example
>
>>> y=[1 2 3 4 4 4 5 6];
>
>This :
>
>if y~=4
> foo = 1
>else
> foo = 0
>end
>
>gives foo = 0, which may seem counter intuitive.
>
>
>To get the "expected" behaviour you need to use the 'any' operator
>
>if any(y~=4)
> foo = 1
>else
> foo = 0
>end
>
>gives foo =1
>
>For matrices rather than vectors, you will need any(any(Y~=4)).
>
>I hope that helps.
>
>Rob
>
>Matt Taylor wrote:
>
>> Arun,
>>
>> It works for me, using ver 5.2. Example:
>>
>> (I;(B y=[1 2 3 4 4 4 5 6]
>> y =
>> 1 2 3 4 4 4 5 6
>> (I;(B y~=4
>> ans =
>> 1 1 1 0 0 0 1 1
>>
>> Matt Taylor
>>
>> Arun Chhabra wrote in message <6uu6n7$s6i$1@dskd3.itg.ti.com>...
>> >*** To respond, remove "no-spam" from the reply-to address ***
>> >
>> >How do you represent "Not Equal To" in Matlab?? Something like the
>> >equivalent of "!=" in C.
>> >
>> >I know that NOT is "~", however the combined expression of "~=" did not
>> >work.
>> >
>> >I'd appreciate any suggestions...I'm using Matlab 4.0.
>> >
>> >Thanks in advance,
>> >Arun
>> >
>> >
>> >
>> >
>
>
>
>--
>===========================================================
>Robert Henson E-mail - rob@mathworks.com
>The MathWorks, Inc. WWW - http://www.mathworks.com
>24 Prime Parkway Phone - (508) 647-7505
>Natick, MA 01760 FAX - (508) 647-7012
>===========================================================
>
>

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