Re: Simple IF statement




"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


.



Relevant Pages

  • Re: Visitors from Down the Street: my review
    ... The president executes it. ... the Congressional power of impeachment. ... Priviledge and pretty much every president from Washington forward has ... And invoked it to keep people from talking to Congress. ...
    (rec.arts.sf.tv.babylon5.moderated)
  • RE: power off button
    ... BIOS settings in your laptop's BIOS. ... > Christopher Cox ... > Subject: power off button ... > Currently it seems as if it executes a halt or shutdown -h ...
    (freebsd-questions)
  • Re: Simple IF statement
    ... My resistive power and acceleration power data points are stored ... > 'braking power' vector element and a zero should now be placed in the ... > appropiate element within the 'traction power' vector. ... executes the stuff after "then", ...
    (comp.soft-sys.matlab)
  • power off button
    ... Is there any way to control what command is executed when I push the power ... Currently it seems as if it executes a halt or shutdown -h when I push the ... In 5.4-RELEASE this does not power my system off ... ...
    (freebsd-questions)
  • Re: Newbie problem
    ... It probably executes it already, ... no newline or explicit flush. ... how command (and obviosuly it doesn't give the variable Objective the ... Consider an A-Z scan of the predicate summary in the documentation. ...
    (comp.lang.prolog)