3 condition in IF statement

Hi everybody. I have program which 3 condition should be compare. I want this IF statement to be true only in the case where the first condition AND the second condition AND the third condition are all true simultaneously.
Thanks a lot

is saying f1 is less than or equal to 0 and less than or equal to 2, so that the "less than or equal to 2" would be redundant. I suspect you mean "0<=f1<=2 which means that f1 lies between 0 and 2. However, this latter notation is not allowed in matlab. You must say "0<=f1 & f1<=2". The same applies to the other five conditions in your code.

"Roger Stafford" wrote in message <kt95up$k6l$1@newscl01ah.mathworks.com>...
> "Babak " <b_mn@hotmail.com> wrote in message <kt94qu$h19$1@newscl01ah.mathworks.com>...
> > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> > disp('Normal)')
> > elseif (0>=f1<=3) && (1>=f2<=5) && (1>=THD<=6)
> > disp('Sag')
> - - - - - - - - - - - -
> Each of your six condition expressions is invalid in matlab syntax. In fact, as they stand, they don't mean what I believe you intend. For example
>
> "0>=f1<=2"
>
> is saying f1 is less than or equal to 0 and less than or equal to 2, so that the "less than or equal to 2" would be redundant. I suspect you mean "0<=f1<=2 which means that f1 lies between 0 and 2. However, this latter notation is not allowed in matlab. You must say "0<=f1 & f1<=2". The same applies to the other five conditions in your code.
>
> Roger Stafford

"Babak " <b_mn@hotmail.com> wrote in message <kt96mq$m84$1@newscl01ah.mathworks.com>...
> "Roger Stafford" wrote in message <kt95up$k6l$1@newscl01ah.mathworks.com>...
> > "Babak " <b_mn@hotmail.com> wrote in message <kt94qu$h19$1@newscl01ah.mathworks.com>...
> > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> > > disp('Normal)')
> > > elseif (0>=f1<=3) && (1>=f2<=5) && (1>=THD<=6)
> > > disp('Sag')
> > - - - - - - - - - - - -
> > Each of your six condition expressions is invalid in matlab syntax. In fact, as they stand, they don't mean what I believe you intend. For example
> >
> > "0>=f1<=2"
> >
> > is saying f1 is less than or equal to 0 and less than or equal to 2, so that the "less than or equal to 2" would be redundant. I suspect you mean "0<=f1<=2 which means that f1 lies between 0 and 2. However, this latter notation is not allowed in matlab.

Just to clarify - unfortunately the latter notation IS allowed. But, as you point-out, it doesn't do what the OP thinks it should.

This might be a good addition for the MATLAB FAQ.

> > You must say "0<=f1 & f1<=2".

To get the intended result.

> > The same applies to the other five conditions in your code.
> >
> > Roger Stafford
>
>
> Thank you Roger. You were right

"Babak " <b_mn@hotmail.com> wrote in message <kt96mq$m84$1@newscl01ah.mathworks.com>...
> > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
- - - - - - -
'Someone' is right. Strictly speaking, that notation is allowed, but it is interpreted in the following remarkable way. In "4>=f2<=5" suppose f2 = 6. Then the expression is parsed as:

(4>=f2)<=5

The "4>=f2" portion would be false which gives "false<=5", but since the "<=" demands numerical values at either end, the 'false' is translated to a numerical 0, which gives "0<=5" and that is actually true. However this is certainly not what one would expect in such a case.

Many people have been tripped up by this kind of unintended result. The obvious remedy is to never, never use expressions of a form similar to a<=b<=c in matlab.

"Roger Stafford" wrote in message <kt9dkd$a5l$1@newscl01ah.mathworks.com>...
> "Babak " <b_mn@hotmail.com> wrote in message <kt96mq$m84$1@newscl01ah.mathworks.com>...
> > > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> - - - - - - -
> 'Someone' is right. Strictly speaking, that notation is allowed, but it is interpreted in the following remarkable way. In "4>=f2<=5" suppose f2 = 6. Then the expression is parsed as:
>
> (4>=f2)<=5
>
> The "4>=f2" portion would be false which gives "false<=5", but since the "<=" demands numerical values at either end, the 'false' is translated to a numerical 0, which gives "0<=5" and that is actually true. However this is certainly not what one would expect in such a case.
>
> Many people have been tripped up by this kind of unintended result. The obvious remedy is to never, never use expressions of a form similar to a<=b<=c in matlab.
>
> Roger Stafford

Very interesting! Thank you for more info,nowhere I could find such info

I don't see it as "remarkable"; it's simply the result of applying
operator precedence rules. It's not _that_ much different in other
languages other than one might get at least a warning in a more strongly
typed language and an outright error in more stringent ones yet.

That it might not mean what OP thought simply means mostly that OP
hadn't yet read the manual... :)

dpb <none@non.net> wrote in message <kt9jfp$cre$1@speranza.aioe.org>...
> I don't see it as "remarkable"; it's simply the result of applying
> operator precedence rules. It's not _that_ much different in other
> languages other than one might get at least a warning in a more strongly
> typed language and an outright error in more stringent ones yet.
>
> That it might not mean what OP thought simply means mostly that OP
> hadn't yet read the manual... :)
- - - - - - - - - -
I said "remarkable" in an endeavor to see things from the point of view of someone familiar with mathematical notation but not with that of matlab. The notation a<=b<=c occurs very frequently in mathematics and is therefore an easy mistake to make for someone in the process of learning matlab. In my opinion using opposing inequalities as in "4>=f2<=5" is a more fundamental error.

> (4>=f2)<=5
>
> The "4>=f2" portion would be false which gives "false<=5", but since the "<=" demands numerical values
>at either end, the 'false' is translated to a numerical 0, which gives "0<=5" and that is actually true.
>However this is certainly not what one would expect in such a case.
>

On 7/30/2013 9:04 PM, Nasser M. Abbasi wrote:
> On 7/30/2013 5:10 PM, Roger Stafford wrote:
>
>> (4>=f2)<=5
>>
>> The "4>=f2" portion would be false which gives "false<=5", but since
>> the "<=" demands numerical values
>> at either end, the 'false' is translated to a numerical 0, which gives
>> "0<=5" and that is actually true.
>> However this is certainly not what one would expect in such a case.
>>
...

Fortran

program precedence
integer :: f=6

write(*,*) 4 >= f <= 5
end program

C:\Temp> df /nologo preced.f90
preced.f90

C:\Temp> preced
T

NB: Above uses a vendor extension that allows the first logical result
of .F. to then be compared to an integer which as in Matlab converts the
internal value of -1 to integer and that is also <=5 so result is .T.

If one verifies Standard conformance, then the silent conversion is
non-conforming and one will get an error w/ a conforming compiler.

dpb <none@non.net> wrote in message <kta2u3$de5$1@speranza.aioe.org>...
> On 7/30/2013 9:04 PM, Nasser M. Abbasi wrote:
> > On 7/30/2013 5:10 PM, Roger Stafford wrote:
> >
> >> (4>=f2)<=5
> >>
> >> The "4>=f2" portion would be false which gives "false<=5", but since
> >> the "<=" demands numerical values
> >> at either end, the 'false' is translated to a numerical 0, which gives
> >> "0<=5" and that is actually true.
> >> However this is certainly not what one would expect in such a case.
> >>
> ...
>
> Fortran
>
> program precedence
> integer :: f=6
>
> write(*,*) 4 >= f <= 5
> end program
>
> C:\Temp> df /nologo preced.f90
> preced.f90
>
> C:\Temp> preced
> T
>
> NB: Above uses a vendor extension that allows the first logical result
> of .F. to then be compared to an integer which as in Matlab converts the
> internal value of -1 to integer and that is also <=5 so result is .T.
>
> If one verifies Standard conformance, then the silent conversion is
> non-conforming and one will get an error w/ a conforming compiler.
>
> --

We are having a Matlab Hall of Fame discussion... Which I am definitely not worthy.

As for Maple 17, I got "false". No error but I always wonder if I am using the correct formats with these later versions.

On 7/30/2013 8:48 PM, Roger Stafford wrote:
> dpb <none@non.net> wrote in message <kt9jfp$cre$1@speranza.aioe.org>...
>> I don't see it as "remarkable"; it's simply the result of applying
>> operator precedence rules. It's not _that_ much different in other
>> languages other than one might get at least a warning in a more
>> strongly typed language and an outright error in more stringent ones yet.
>>
>> That it might not mean what OP thought simply means mostly that OP
>> hadn't yet read the manual... :)
> - - - - - - - - - -
> I said "remarkable" in an endeavor to see things from the point of view
> of someone familiar with mathematical notation but not with that of
> matlab. The notation a<=b<=c occurs very frequently in mathematics and
> is therefore an easy mistake to make for someone in the process of
> learning matlab. In my opinion using opposing inequalities as in
> "4>=f2<=5" is a more fundamental error.
>
> Roger Stafford

I voted against "remarkable" owing to the 60-yr precedent of FORTRAN for
precisely the same implementation/interpretation (w/ the noted extension
of casting a LOGICAL to INTEGER which is at least moderately common if
not universal).

I would presume it's that Matlab began as being implemented in FORTRAN
that caused such specific interpretation and that it has remained that
way for consistency's sake as much or more than for any other reason to
change.

Perhaps my background colors my observations too much that I can't
separate out that programming in any language _isn't_ mathematics
despite how much (or little) any given one tries to mimic mathematical
operations. Naming it FORmulaTRANslation, after all, is pretty close to
saying they tried. :)

"Babak " <b_mn@hotmail.com> wrote in message
news:kt9ee8$bt7$1@newscl01ah.mathworks.com...
> "Roger Stafford" wrote in message
> <kt9dkd$a5l$1@newscl01ah.mathworks.com>...
>> "Babak " <b_mn@hotmail.com> wrote in message
>> <kt96mq$m84$1@newscl01ah.mathworks.com>...
>> > > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
>> - - - - - - -
>> 'Someone' is right. Strictly speaking, that notation is allowed, but
>> it is interpreted in the following remarkable way. In "4>=f2<=5" suppose
>> f2 = 6. Then the expression is parsed as:
>>
>> (4>=f2)<=5
>>
>> The "4>=f2" portion would be false which gives "false<=5", but since the
>> "<=" demands numerical values at either end, the 'false' is translated to
>> a numerical 0, which gives "0<=5" and that is actually true. However
>> this is certainly not what one would expect in such a case.
>>
>> Many people have been tripped up by this kind of unintended result.
>> The obvious remedy is to never, never use expressions of a form similar
>> to a<=b<=c in matlab.
>>
>> Roger Stafford
>
> Very interesting! Thank you for more info,nowhere I could find such info

"Loren Shure" wrote in message <ktdkfo$nvs$1@newscl01ah.mathworks.com>...
>
> "Babak " <b_mn@hotmail.com> wrote in message
> news:kt9ee8$bt7$1@newscl01ah.mathworks.com...
> > "Roger Stafford" wrote in message
> > <kt9dkd$a5l$1@newscl01ah.mathworks.com>...
> >> "Babak " <b_mn@hotmail.com> wrote in message
> >> <kt96mq$m84$1@newscl01ah.mathworks.com>...
> >> > > > if ((0>=f1<=2) && (4>=f2<=5) && (1>=THD<=6))
> >> - - - - - - -
> >> 'Someone' is right. Strictly speaking, that notation is allowed, but
> >> it is interpreted in the following remarkable way. In "4>=f2<=5" suppose
> >> f2 = 6. Then the expression is parsed as:
> >>
> >> (4>=f2)<=5
> >>
> >> The "4>=f2" portion would be false which gives "false<=5", but since the
> >> "<=" demands numerical values at either end, the 'false' is translated to
> >> a numerical 0, which gives "0<=5" and that is actually true. However
> >> this is certainly not what one would expect in such a case.
> >>
> >> Many people have been tripped up by this kind of unintended result.
> >> The obvious remedy is to never, never use expressions of a form similar
> >> to a<=b<=c in matlab.
> >>
> >> Roger Stafford
> >
> > Very interesting! Thank you for more info,nowhere I could find such info
>
> I wrote about this on my blog last year:
>
> http://blogs.mathworks.com/loren/2012/05/14/why-is-answer-to-3-a-7-unexpected/
>
> --
> --Loren
>
> http://blogs.mathworks.com/loren

I think it would be nice to have Loren's discussion (or a paraphrase of it) in the MATLAB FAQ.

What is a watch list?

You can think of your watch list as threads that you have bookmarked.

You can add tags, authors, threads, and even search results to your watch list. This way you can easily keep track of topics that you're interested in. To view your watch list, click on the "My Newsreader" link.

To add items to your watch list, click the "add to watch list" link at the bottom of any page.

How do I add an item to my watch list?

Search

To add search criteria to your watch list, search for the desired term in the search box. Click on the "Add this search to my watch list" link on the search results page.

You can also add a tag to your watch list by searching for the tag with the directive "tag:tag_name" where tag_name is the name of the tag you would like to watch.

Author

To add an author to your watch list, go to the author's profile page and click on the "Add this author to my watch list" link at the top of the page. You can also add an author to your watch list by going to a thread that the author has posted to and clicking on the "Add this author to my watch list" link. You will be notified whenever the author makes a post.

Thread

To add a thread to your watch list, go to the thread page and click the "Add this thread to my watch list" link at the top of the page.

About Newsgroups, Newsreaders, and MATLAB Central

What are newsgroups?

The newsgroups are a worldwide forum that is open to everyone. Newsgroups are used to discuss a huge range of topics, make announcements, and trade files.

Discussions are threaded, or grouped in a way that allows you to read a posted message and all of its replies in chronological order. This makes it easy to follow the thread of the conversation, and to see what’s already been said before you post your own reply or make a new posting.

Newsgroup content is distributed by servers hosted by various organizations on the Internet. Messages are exchanged and managed using open-standard protocols. No single entity “owns” the newsgroups.

There are thousands of newsgroups, each addressing a single topic or area of interest. The MATLAB Central Newsreader posts and displays messages in the comp.soft-sys.matlab newsgroup.

How do I read or post to the newsgroups?

MATLAB Central

You can use the integrated newsreader at the MATLAB Central website to read and post messages in this newsgroup. MATLAB Central is hosted by MathWorks.

Messages posted through the MATLAB Central Newsreader are seen by everyone using the newsgroups, regardless of how they access the newsgroups. There are several advantages to using MATLAB Central.

Use the Email Address of Your Choice
The MATLAB Central Newsreader allows you to define an alternative email address as your posting address, avoiding clutter in your primary mailbox and reducing spam.

Spam Control
Most newsgroup spam is filtered out by the MATLAB Central Newsreader.

Tagging
Messages can be tagged with a relevant label by any signed-in user. Tags can be used as keywords to find particular files of interest, or as a way to categorize your bookmarked postings. You may choose to allow others to view your tags, and you can view or search others’ tags as well as those of the community at large. Tagging provides a way to see both the big trends and the smaller, more obscure ideas and applications.

Watch lists
Setting up watch lists allows you to be notified of updates made to postings selected by author, thread, or any search variable. Your watch list notifications can be sent by email (daily digest or immediate), displayed in My Newsreader, or sent via RSS feed.

Other ways to access the newsgroups

Use a newsreader through your school, employer, or internet service provider

Pay for newsgroup access from a commercial provider

Use Google Groups

Mathforum.org provides a newsreader with access to the comp.soft sys.matlab newsgroup