For..Do Loop and If statements [message #94095] |
Wed, 18 January 2017 21:18  |
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   |
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 #94102 is a reply to message #94100] |
Thu, 19 January 2017 09:43  |
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
|
|
|