经常在IDL中遇到需要从大的二维数组中需要提取小的二维数组的情况。这里用矩阵乘法写了一个函数,可以实现从大的数组中提取子数组。这个函数返回的是要提取的数组的下标。
;从大的二维数组中提取一块区域
; r 原数组的行数
; c 原数组的列数
; ro 子数组在原数组中的起始行号
; co 子数组在原数组中的起始列号
; rs 子数组的行数
; cs 了数组的列数
function getIndex, r, c, ro, co, rs, cs
rb = lindgen(cs, start=1,increment=0) # lindgen(rs, start=0, increment=c)
cb = lindgen(cs, increment=1) # indgen(rs, start=1,increment=0)
return, rb + cb + ro * c + co
end
调用示例如下:
pro test
r = 13
c = 11
dat = indgen(c,r)
ro = 1
co = 1
rs = 7
cs = 5
index = getIndex(r, c, ro, co, rs, cs)
;print,pos
print,index
end
调用结果如下图:
test过程的输出如下:
12 13 14 15 16
23 24 25 26 27
34 35 36 37 38
45 46 47 48 49
56 57 58 59 60
67 68 69 70 71
78 79 80 81 82
这样做的好处是速度比使用循环要快一点,缺点是使用的内存会多一些。