Re: If statement



Thanks,

Actually, i want to compare atabs(i) in equality
comparasion. where atabs(i) is a vector.

If i try using atabs is a scalar i can us atabs==0, but if
vector i can't. so what should i do ?



roberson@xxxxxxxxxxxxxxxxxx (Walter Roberson) wrote in
message <g4dlis$nm0$1@xxxxxxxxxxxxxxxxxxxxxxx>...
In article <g4dfnq$9mn$1@xxxxxxxxxxxxxxxxxx>,
Didi Istardi <istardi@xxxxxxxxx> wrote:

i have problem with my if statement program.

if atabs(i):0

the colon operator is the range construction operator.
For example, 3:5 constructs the vector [3 4 5] .

atabs(i):0 would thus look up the value of atabs(i)
or call the function atabs passing in the argument i
(we cannot tell from your code whether atabs is a function
or any array). Once it had that value, it would construct
the vector starting from that value and incrementing by 1
until the final value was less than or equal to 0.
For example, if atabs(i) was -3.2 then it would construct
the vector [-3.2 -2.2 -1.2 -0.2] If atabs(i) was already
greater than 0 then the empty vector would be constructed.

Once the vector had been constructed, it would be the operand
to the 'if' statement. When the operand to an 'if' statement
is a vector, then the 'if' statement is considered to succeed
if all values in the vector are non-zero, just as if the
test had been written originally as

if all(atabs(i):0)

all() applied to the empty vector is false, so if atabs(i)
is greater than 0 (leading to the empty vector) then the 'if'
statement will be false. If atabs(i) is exactly 0, then the
vector containing just the value 0 would be constructed, and
since that value is *not* non-zero, the test would be false.
If atabs(i) is a negative integer, then a vector such as
[-3 -2 -1 0] would be constructed; the initial values of that
vector are non-zero, but the final value of that vector
would always
be 0, so not *all* of the vector values would be non-zero
so the
if would be considered to fail.

The one case in which the test would succeed is if atabs(i)
is a negative value which has a non-zero fractional part,
such as the -3.2 I showed above. All of the values in
[-3.2 -2.2 -1.2 -0.2] are non-zero, so the test would succeed.


What you have coded, then, is an unusual short-hand for what
would normally be written as:

if (atabs(i) > 0) && (mod(atabs(i),1) == 0)

Unless, that is, atabs(i) is a function, in which case the
equivilent code would be:

temp = atabs(i); if (temp > 0) && (mod(temp,1) == 0)

This is probably not what you want.


I am not sure what you -do- want to test in that particular
statement. If I recall correctly, though, the old DEC VT220
and VT320 series of terminals used to have the : character
as the shifted value on the > key, so there is a -marginally-
higher chance that what you meant to code was

if atabs(i)>0

Others might argue, though, that the : character could be the
result of misreading a hand-written = character in written
pseudo-code for a routine. The Matlab operator to test for
equality is == (two equal signs), so if it was meant to be
an equality comparison, then the code would be

if atabs(i)==0
--
"Ignorance has been our king... he sits unchallenged on
the throne of
Man. His dynasty is age-old. His right to rule is now
considered
legitimate. Past sages have affirmed it. They did
nothing to unseat
him." -- Walter M
Miller, Jr

.



Relevant Pages

  • Re: collection framework: using the good interface
    ... The word object's class would implement Comparable, and should have equals and hashCode consistent with compareTo, but I would not make two words equal unless they are, in all respects, the same word. ... I'm more and more convinced that equality should involve all aspects of the Word class, but I'm still uncomfortable with mixing lexicographic information (allowing to compare objects in different collections) and information about frequency, distribution, cooccurrents. ...
    (comp.lang.java.help)
  • Re: [CLOS] Ensuring a method exists
    ... application wants to compare. ... I said "depending on context" for a reason: I think something like dynamically scoped functions or ContextL can be of use here. ... The equality operators in CL are generic in the sense that they are ...
    (comp.lang.lisp)
  • Re: Glasgow haskell vs. Lispworks
    ... 'dbenson AT eecs DOT wsu DOT edu' wrote: ... Jon simply has been using the object identity instead of value ... The equality on objects is said to be object ... This is useful to compare ...
    (comp.lang.functional)
  • Re: Why keep identity-based equality comparison?
    ... >> equality operator for Py3K, ... Ditto for container methods that ... Whenever you compare two objects that ... > don't have the same type, you'll get an exception and terminate the ...
    (comp.lang.python)
  • Re: If statement
    ... i want to compare atabsin equality ... allowing some tolerance would make sense, e.g., abs<= tolerance ...
    (comp.soft-sys.matlab)