Re: If statement
- From: "Didi Istardi" <istardi@xxxxxxxxx>
- Date: Tue, 1 Jul 2008 19:05:03 +0000 (UTC)
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>,would always
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
be 0, so not *all* of the vector values would be non-zeroso the
if would be considered to fail.the throne of
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
Man. His dynasty is age-old. His right to rule is nowconsidered
legitimate. Past sages have affirmed it. They didnothing to unseat
him." -- Walter MMiller, Jr
.
- Follow-Ups:
- Re: If statement
- From: Walter Roberson
- Re: If statement
- From: Loren Shure
- Re: If statement
- References:
- If statement
- From: Didi Istardi
- Re: If statement
- From: Walter Roberson
- If statement
- Prev by Date: Re: Adding Multiple Images on a Base Image
- Next by Date: Vectorization for Quadratic Polynomial Regression
- Previous by thread: Re: If statement
- Next by thread: Re: If statement
- Index(es):
Relevant Pages
|