If statement IDL检查数组中的数字是否正确

If statement IDL检查数组中的数字是否正确,if-statement,idl-programming-language,If Statement,Idl Programming Language,我对IDL很陌生 实际上,我想要做的是使用if语句检查当前索引i是否在数组中 在Python中,它将类似于以下内容: if this_num in xartifact: print 'Is an x artifact' elif this_num in yartifact: print 'Is a y artifact' else: print 'Is neither' 我知道您可以在IDL中嵌套ifs: IF P1 THEN S1 ELSE $ IF P2 THEN S2

我对IDL很陌生

实际上,我想要做的是使用if语句检查当前索引i是否在数组中

在Python中,它将类似于以下内容:

if this_num in xartifact:
   print 'Is an x artifact'
elif this_num in yartifact:
   print 'Is a y artifact'
else:
   print 'Is neither'
我知道您可以在IDL中嵌套ifs:

IF P1 THEN S1 ELSE $

IF P2 THEN S2 ELSE $

IF PN THEN SN ELSE SX
我只是不知道是否有一个在运营商或理智的方式这样做


欢呼声

IDL在使用if语句时可能会有点过度紧张。正如你所说,基本的“if-then-else-if-then”语句可能是这样的:

if a eq 0 then print, 'the variable a equals 0' else $
if a eq 1 then print, 'the variable a equals 1' $
else print, 'the variable is something else'
if a eq 0 then begin
  print, 'the variable a equals 0'
  print, 'more stuff on this line'
endif else if a eq 1 then begin
  print, 'the variable a equals 1'
  print, 'another line'
endif else begin
  print, 'a is something else'
  print, 'yet another line'
endelse
对于if语句中的多行,您可以使用以下命令,而不是使用$继续该行:

if a eq 0 then print, 'the variable a equals 0' else $
if a eq 1 then print, 'the variable a equals 1' $
else print, 'the variable is something else'
if a eq 0 then begin
  print, 'the variable a equals 0'
  print, 'more stuff on this line'
endif else if a eq 1 then begin
  print, 'the variable a equals 1'
  print, 'another line'
endif else begin
  print, 'a is something else'
  print, 'yet another line'
endelse
最后,评估变量是否在向量中取决于您想要做什么以及数组中有什么,但一个选项是使用where函数。一个示例演示了它的工作原理:

a=2
b=[1,2,2,3]
result = where(a eq b)
print, result
if result[0] ne -1 then print, 'a is in b' $
else print, 'a is not in b'

也许还有更好的方法可以做到这一点。可能是一个case语句。

IDL可能会对if语句有点过度紧张。正如你所说,基本的“if-then-else-if-then”语句可能是这样的:

if a eq 0 then print, 'the variable a equals 0' else $
if a eq 1 then print, 'the variable a equals 1' $
else print, 'the variable is something else'
if a eq 0 then begin
  print, 'the variable a equals 0'
  print, 'more stuff on this line'
endif else if a eq 1 then begin
  print, 'the variable a equals 1'
  print, 'another line'
endif else begin
  print, 'a is something else'
  print, 'yet another line'
endelse
对于if语句中的多行,您可以使用以下命令,而不是使用$继续该行:

if a eq 0 then print, 'the variable a equals 0' else $
if a eq 1 then print, 'the variable a equals 1' $
else print, 'the variable is something else'
if a eq 0 then begin
  print, 'the variable a equals 0'
  print, 'more stuff on this line'
endif else if a eq 1 then begin
  print, 'the variable a equals 1'
  print, 'another line'
endif else begin
  print, 'a is something else'
  print, 'yet another line'
endelse
最后,评估变量是否在向量中取决于您想要做什么以及数组中有什么,但一个选项是使用where函数。一个示例演示了它的工作原理:

a=2
b=[1,2,2,3]
result = where(a eq b)
print, result
if result[0] ne -1 then print, 'a is in b' $
else print, 'a is not in b'

也许还有更好的方法可以做到这一点。可能是一个案例陈述。

我会在
中使用
count
参数,其中
类似于上面的示例:

a = 2
b = [1, 2, 3, 5]
ind = where(a eq b, count)
print, count gt 0 ? 'a in b' : 'a not in b'

我会在
的WHERE
中使用
count
参数,类似于上面的示例:

a = 2
b = [1, 2, 3, 5]
ind = where(a eq b, count)
print, count gt 0 ? 'a in b' : 'a not in b'

@mgalloy提供的答案肯定有效,但是有一个更简单的解决方案,它利用了整个过程,只涉及一行代码

a = 2
b = [1, 2, 3, 5]

if total(b eq a) eq 1 then print, 'Yes' else print, 'No'

@mgalloy提供的答案肯定有效,但是有一个更简单的解决方案,它利用了整个过程,只涉及一行代码

a = 2
b = [1, 2, 3, 5]

if total(b eq a) eq 1 then print, 'Yes' else print, 'No'

参考上一个示例,您还可以减少行数(这当然会降低可读性):
如果((a eq b))[0]ne-1)然后打印“a在b中”$
。这节省了一行,您不需要将
where
的结果存储在变量
result
中。参考上一个示例,您还可以减少行数(这当然会降低可读性):
如果((其中(a eq b))[0]ne-1)然后打印“a在b中”$
。这将保存一行,并且您不需要将
where
的结果存储在变量
result