Re: error using statement: If.....then [message #54543] |
Tue, 26 June 2007 20:27 |
David Fanning
Messages: 11724 Registered: August 2001
|
Senior Member |
|
|
Ed Hyer writes:
> IDL also provides many good ways to do this logical operation without
> loops or explicit IF/THEN, such as:
>
> x = ( x > 0) + (x lt 0);
> x = (x*(x ge 0)) + (x lt 0);
> x = (x > 1) - ( x eq 0);
>
> , all of which accomplish what you're trying to do here.
I wonder if the error in your previous article couldn't be
caused by obfuscated code. :-)
Cheers,
David
--
David Fanning, Ph.D.
Fanning Software Consulting, Inc.
Coyote's Guide to IDL Programming: http://www.dfanning.com/
Sepore ma de ni thui. ("Perhaps thou speakest truth.")
|
|
|
Re: error using statement: If.....then [message #54548 is a reply to message #54543] |
Tue, 26 June 2007 17:50  |
MarioIncandenza
Messages: 231 Registered: February 2005
|
Senior Member |
|
|
>> IF x LT 0 then x = 1
IDL also provides many good ways to do this logical operation without
loops or explicit IF/THEN, such as:
x = ( x > 0) + (x lt 0);
x = (x*(x ge 0)) + (x lt 0);
x = (x > 1) - ( x eq 0);
, all of which accomplish what you're trying to do here.
|
|
|
Re: error using statement: If.....then [message #54551 is a reply to message #54548] |
Tue, 26 June 2007 14:30  |
Michael Galloy
Messages: 1114 Registered: April 2006
|
Senior Member |
|
|
On Jun 26, 2:55 pm, Giuseppe Giovanni Salerno <g...@cam.ac.uk> wrote:
> Hi everybody, I am new on IDL and I will be very grateful if someone
> could help me
>
> I have an array of value (X,Y) and I want all Y data negative equals
> to 1:
> x y
> 1 84
> 2 90
> 3 14
> 4 -75
> 5 -95
> 6 -27
>
> so I guess the correct way to apply this filtering is working with IF
> and THEN:
>
> IF x LT 0 then x = 1
>
> but I got an error:
> "Expression must be a scalar or 1 element array in this context: <BYTE
> Array[2, 6]"
> How I can resolve this problem?
>
> Thanks in advance
> Cico
A couple points:
1. Array indices start at 0 in IDL, so the x column in your table of
values should be 0..5.
2. The technique you are trying above (i.e. with an IF statement) also
needs a FOR loop, to loop over each individual element. This is not
the best technique for IDL.
3. Use the WHERE statement to do things like the above. How about:
negIndices = where(x lt 0, nNegative)
if (nNegative gt 0) then x[negIndices] = 1
This could be done like:
x[where(x lt 0)] = 1
if you know that there will always be some negative values in x. (But
I would always check.)
Mike
--
www.michaelgalloy.com
|
|
|