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

Home » Public Forums » archive » For..Do Loop and If statements
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
For..Do Loop and If statements [message #94095] Wed, 18 January 2017 21:18 Go to next message
smnadoum is currently offline  smnadoum
Messages: 24
Registered: June 2016
Junior Member
I have written a couple of for.. do loops, however, they don't work properly. Not sure what I did wrong.

1) I want to use a for..do loop and if statements to derive indices. where 'a' is greater than 5, and output the result to new variable 'b2'

a is INT = Array[4, 5, 3]
b2=[a]
b2=[]
for i=0, n_elements(a) -1 do begin
temp =a[i]
if temp gt 5 then b2=[b2,temp]

2) I want to loop through the elements of d and use an if statement: if the element of d is equal to 20 then divid that element by a floating point 5, and if the element is not equal to 20 then divide it by a floating point 3.


d is INT = Array[4, 5]
for i=0, n_elements(d)-1 do begin ;if statement eq to 20 then divid by 5. ne 20 then divide by 3.
if d(i) eq 20 then print, 20/5.
if d(i) ne 20 then print, 20/3.
endfor
end


None of these statements work and I don't know why.
Re: For..Do Loop and If statements [message #94098 is a reply to message #94095] Thu, 19 January 2017 01:06 Go to previous messageGo to next message
Helder Marchetto is currently offline  Helder Marchetto
Messages: 520
Registered: November 2011
Senior Member
On Thursday, January 19, 2017 at 6:18:27 AM UTC+1, Cheryl wrote:
> I have written a couple of for.. do loops, however, they don't work properly. Not sure what I did wrong.
>
> 1) I want to use a for..do loop and if statements to derive indices. where 'a' is greater than 5, and output the result to new variable 'b2'
>
> a is INT = Array[4, 5, 3]
> b2=[a]
> b2=[]
> for i=0, n_elements(a) -1 do begin
> temp =a[i]
> if temp gt 5 then b2=[b2,temp]
>
> 2) I want to loop through the elements of d and use an if statement: if the element of d is equal to 20 then divid that element by a floating point 5, and if the element is not equal to 20 then divide it by a floating point 3.
>
>
> d is INT = Array[4, 5]
> for i=0, n_elements(d)-1 do begin ;if statement eq to 20 then divid by 5. ne 20 then divide by 3.
> if d(i) eq 20 then print, 20/5.
> if d(i) ne 20 then print, 20/3.
> endfor
> end
>
>
> None of these statements work and I don't know why.

Hi,
I think you should get a book of basic IDL programming or programming in general.

Here are some comments to your code:
> a is INT = Array[4, 5, 3]
> b2=[a] ;unnecessary: remove this statement
> b2=[]
> for i=0, n_elements(a) -1 do begin
> temp =a[i]
> if temp gt 5 then b2=[b2,temp]
at the end of the loop, you need an "endfor" statement

That should then work. However, IDL is not made for loops. The typical way to do this is to use the where function (http://www.harrisgeospatial.com/docs/WHERE.html):

b2 = a[where(a gt 5)]

if you're not sure if any elements of a are greater than 5, then you should use:
pos = where(a gt 5, cnt)
if cnt gt 0 then b2 = a[pos] else b2 = []

Your second example does not make much sense. 20/5. is 4 and 20/3. is 6.666... Now, you don't need to recalculate this every time in the loop. So:

result_one = 20/5.
result_two = 20/3.

> d is INT = Array[4, 5]
> for i=0, n_elements(d)-1 do begin ;if statement eq to 20 then divid by 5. ne 20 then divide by 3.
> if d(i) eq 20 then print, 20/5. ;use square brackets!!! d[i]
> if d(i) ne 20 then print, 20/3. ;use square brackets!!! d[i]
; the "if" condition has also a "else" option. Meaning: "if something is eq to 20 then do this, if not (else!) do that". See http://www.harrisgeospatial.com/docs/IF___THEN___ELSE.html
> endfor
> end ; erase this line

This can also be done with the where function. Have a look at the help and try to figure it out. Let me know if you can't manage that.

Cheers,
Helder
Re: For..Do Loop and If statements [message #94100 is a reply to message #94098] Thu, 19 January 2017 02:44 Go to previous messageGo to next message
Markus Schmassmann is currently offline  Markus Schmassmann
Messages: 129
Registered: April 2016
Senior Member
On 01/19/2017 10:06 AM, Helder wrote:
> That should then work. However, IDL is not made for loops. The
> typical way to do this is to use the where function
> (http://www.harrisgeospatial.com/docs/WHERE.html):
>
> b2 = a[where(a gt 5)]
>
> if you're not sure if any elements of a are greater than 5, then you
> should use: pos = where(a gt 5, cnt) if cnt gt 0 then b2 = a[pos]
> else b2 = []
A small addition to Helder's answer:
if there might be elements greater than 5, you could also use:

b2 = a[where(a gt 5,/NULL)]

this avoids the use of if...then, which is rather slow in IDL

Markus
Re: For..Do Loop and If statements [message #94102 is a reply to message #94100] Thu, 19 January 2017 09:43 Go to previous message
Heinz Stege is currently offline  Heinz Stege
Messages: 189
Registered: January 2003
Senior Member
On Thu, 19 Jan 2017 11:44:14 +0100, Markus Schmassmann wrote:

> ... the use of if...then, which is rather slow in IDL
>
No, this is not true. The if...then statement is negligible in
comparison to a where-statement.

Cheers, Heinz
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: IDL 8.6 demo mode
Next Topic: How to run correl_optimize IDL code

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

Current Time: Wed Oct 08 09:22:33 PDT 2025

Total time taken to generate the page: 0.00675 seconds