IDL——变量、数组

IDL在pro...end间定义的局部变量运行后系统自动销毁

如果需要在下一个块中调用,有两种方法

;法一
pro helloidl
a=10
DEFSYSV,'!a',100   ;defsysv定义系统变量,命名时以!开头,后为变量值
print,a
myfun01            ;在前一个块中声明myfun01
end

pro myfun01
print,!a
end
;法二
pro helloidl
common comName,a   ;定义公共空间,comName空间名称 
a=10
print,a
myfun01
end

pro myfun01
common comName,a   ;调用时需声明
print,a
end

IDL数组

定义时,先列后行、步长在后

其运算规则于matlab类似

IDL> arr=[1,2,3,4]
IDL> arr
       1       2       3       4

IDL> arr=[[1,2,3],[4,5,6]]
IDL> arr
       1       2       3
       4       5       6

IDL> arr=[[1,2,3],[4,5,6D]]
IDL> arr
       1.0000000000000000       2.0000000000000000       3.0000000000000000
       4.0000000000000000       5.0000000000000000       6.0000000000000000

IDL> arr=bytarr(3,2)                          ;IDL中数组先列后行
IDL> arr
   0   0   0
   0   0   0

IDL> make_array(3,2,value=5,type=1)           ;2×3的矩阵,初值为5,byte型
   5   5   5
   5   5   5
IDL> help,make_array(3,2,value=5,type=1)
<Expression>    BYTE      = Array[3, 2]

IDL> arr=[1:10:2]                             ;IDL步长在后
IDL> arr
       1       3       5       7       9
IDL> arr=indgen(4,3)
IDL> arr
       0       1       2       3
       4       5       6       7
       8       9      10      11
IDL> arr[[1,3],*]
       1       3
       5       7
       9      11
IDL> arr>5                                    ;arr>5既使数组中小于5的数替换为5
       5       5       5       5
       5       5       6       7
       8       9      10      11
IDL> arr<5
       0       1       2       3
       4       5       5       5
       5       5       5       5

接着数组,IDL数组运算规则很多,而且因为IDL是针对图像处理的,其y为横轴,x为纵轴,所以数组运算时需要做一定反转。

IDL> arr=[1,2,3,4,3,2,1]           
IDL> where(arr eq 2)                ;输出arr数组中等于2的值的下标
           1           5

IDL> arr=indgen(3,2)
IDL> arr
       0       1       2
       3       4       5
IDL> reform(arr,2,3)                ;reform函数改变数组维度
       0       1
       2       3
       4       5
IDL> arr=indgen(1,3,4)
IDL> arr
       0
       1
       2

       3
       4
       5

       6
       7
       8

       9
      10
      11
IDL> reform(arr,3,4)                ;三维数组改二维数组
       0       1       2
       3       4       5
       6       7       8
       9      10      11

IDL> arr=indgen(2,2)                
IDL> print,arr
       0       1
       2       3
IDL> ;rebin函数中的维度只能是原数组维度的倍数,不能随意指定
IDL> rebin(arr,4,4)  ;默认使用双线性内插法补全
       0       0       1       1
       1       1       2       2
       2       2       3       3
       2       2       3       3
IDL> rebin(arr,6,6)
       0       0       0       1       1       1
       0       0       0       1       1       1
       1       1       1       2       2       2
       2       2       2       3       3       3
       2       2       2       3       3       3
       2       2       2       3       3       3
IDL> rebin(arr,4,4,/sample)  ;最近邻域法
       0       0       1       1
       0       0       1       1
       2       2       3       3
       2       2       3       3
IDL> congrid(arr,3,4)  ;默认最近邻域
       0       1       1
       0       1       1
       2       3       3
       2       3       3
IDL> congrid(arr,3,4,/int)  ;双线性内插法
       0       0       1
       1       1       2
       2       2       3
       2       2       3

IDL> reverse(arr,1)    ;数组反转,1行反转,2列反转
       1       0
       3       2
IDL> reverse(arr,2)    ;在数字图像处理中横轴为y纵轴为x
       2       3
       0       1

IDL> b=[3,2,5,1,5,2]
IDL> sort(b)           ;获取排序后数组的下标
           3           5           1           0           4           2
IDL> b(sort(b))
       1       2       2       3       5       5

IDL> c=[2,2,3,3,2,2,2,2,3,3]
IDL> uniq(c)           ;获取最近唯一值下标
           1           3           7           9
IDL> c(uniq(c))
       2       3       2       3

IDL> str='abcdfe'
IDL> byte(str)         ;获取每个字母的ASCII码
  97  98  99 100 102 101
IDL> string(98B)       ;通过ASCII码输出字母,需要加B
b
IDL> string(reverse(byte(str)))
efdcba

IDL> e=[12,43,21,45,32]
IDL> max(e)
      45
IDL> min(e)
      12
IDL> max(e,min=p)   ;同时可以求出最小值,从左向右赋值
      45
IDL> p
      12

IDL> factorial(4)   ;求阶乘
       24.000000000000000
IDL> product([1:4]) ;连乘
       24.000000000000000
IDL> variance(indgen(3,4))  ;求方差
       13.000000

IDL> a=indgen(2,3)
IDL> b=indgen(3,4)
IDL> a#b             ;矩阵相乘
          10          13
          28          40
          46          67
          64          94
IDL> b##a            ;与a#b互为逆运算结果相同
          10          13
          28          40
          46          67
          64          94
  • 5
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值