comp.lang.idl-pvwave archive
Messages from Usenet group comp.lang.idl-pvwave, compiled by Paulo Penteado

Home » Public Forums » archive » Re: error using statement: If.....then
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
Re: error using statement: If.....then [message #54543] Tue, 26 June 2007 20:27
David Fanning is currently offline  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 Go to previous message
MarioIncandenza is currently offline  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 Go to previous message
Michael Galloy is currently offline  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
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Re: loading colors in tvscl
Next Topic: Segmentation fault: IDL Version 6.3 (linux x86 m32)+Debian SID

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Thu Oct 09 07:03:07 PDT 2025

Total time taken to generate the page: 0.80072 seconds