Re: Simple IF statement
- From: "Steven Lord" <slord@xxxxxxxxxxxxx>
- Date: Tue, 20 Dec 2005 12:57:14 -0500
"Randy Poe" <poespam-trap@xxxxxxxxx> wrote in message
news:1135094731.794025.251680@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
>
> Jay Schmidt wrote:
>> Hi Randy,
>>
>> thanks for your help.
>>
>> Let me explain again what exactly I have got and what doesn't work.
>>
>> 1.) My resistive power and acceleration power data points are stored
>> in several hundred vector elements and if they are summed up it is
>> the total power required to propel the train. Two cases are possible
>> and let me show you those via numerical examples:
*snip rest of explanation*
> OK. Christopher Hulbert's response gives the proper vector
> statements to do what you want to do. Let me instead see
> if I can explain what was going on here.
>
> The if ... then... else tests one thing. If that thing is true, it
> executes the stuff after "then", otherwise it executes the
> thing after "else". You shouldn't think of this statement as
> doing a hundred tests and executing the "then" a hundred
> times. It goes through once.
Correct.
> So when Matlab encounters a statement like this:
>
> if total_power>0
>
> but total_power is a vector, so (total_power>0) is a vector,
> it has to have some rule for deciding whether to treat this
> as a single "false" or a single "true". I think what happens
> is that it acts as if it is false. That is
Almost. As documented in the Nonscalar Expressions section of the
documentation for the IF function:
http://www.mathworks.com/access/helpdesk/help/techdoc/ref/if.html
if the expression that IF is given is nonscalar, then every element of that
expression must be true (or must be nonzero, which when converted to a
logical value becomes true) in order for the IF to be satisfied. If even
one element of the expression is false (or zero, which becomes false when
converted to logical) then the IF statement is not satisfied. So if one
element of total_power is less than or equal to 0, the IF statement above is
not satisfied, so we would drop into the ELSE block if one exists.
As an example, this will display "All elements are >0":
if (1:10) > 0
disp('All elements are > 0');
else
disp('At least one element is <= 0')
end
This will display "At least one element is <= 1":
if (1:10) > 1
disp('All elements are > 1');
else
disp('At least one element is <= 1')
end
> if (vector expression) then... else... end
>
> will always end up executing the "else" block.
Unless "all(vector expression)" returns true, yes.
> You have to think of vectors as single entities, and
> vector statements as things that execute once.
>
> (total_power>0) is a vector of 1's and 0's, and you can
> treat it like any other vector. It happens to be of "logical"
> type which means that it can be used for logical indexing
> as in Christopher's solution. You can do things like
> this:
>
> test = total_power > 0;
> num_positive = sum(test);
> num_negative = sum(~test);
>
> You'll see that (total_power>0) and (total_power<=0) are
> used as indexes in Christopher's solution. Vectors can
> be used as indexes, and when logical vectors are used
> as indexes, they pick out all the elements where the vector
> is true.
To the OP:
What Randy has described is called logical indexing. It's a powerful tool
that I recommend you add to your MATLAB bag of tricks. Getting back to the
IF statement, I'm guessing you thought the IF would operate like a combined
FOR loop and scalar IF statement if given a vector expression to test, but
it doesn't. I'm curious, though -- was there anything in the documentation
or manuals that gave you that impression? Is there some way you think we
can make the behavior of IF statements on vectors more clear, either via
documentation or through some other method?
--
Steve Lord
slord@xxxxxxxxxxxxx
.
- Follow-Ups:
- Re: Simple IF statement
- From: Jay Schmidt
- Re: Simple IF statement
- References:
- Simple IF statement
- From: Jay Schmidt
- Re: Simple IF statement
- From: Rune Allnor
- Re: Simple IF statement
- From: Randy Poe
- Re: Simple IF statement
- From: Jay Schmidt
- Re: Simple IF statement
- From: Randy Poe
- Simple IF statement
- Prev by Date: Re: MATLAB CODE FOR IMAGE PROCESSING
- Next by Date: parabolic interpolation
- Previous by thread: Re: Simple IF statement
- Next by thread: Re: Simple IF statement
- Index(es):
Relevant Pages
|