“数组”的版本间差异

来自Shiyin's note
跳到导航 跳到搜索
无编辑摘要
 
(未显示2个用户的11个中间版本)
第1行: 第1行:
==数组==
==数组==
* REFORM
* REFORM 数组变形
:将数组变为制定的维数
:数组变形,比如data是读入的二维数组,data的第一列可以用 A=reform(data[1,*])
A=indgen(20), B=reform(A,2,10)
::data[1,*]仍然是个二维数组,reform在默认的情况下将这其变为一维数组
:reform在默认(无输入参数)的情况下将数组变为一维
A=reform(data[1,*])

* 中位值
a=median(A,/even);偶数位的数据,取中间两个数的平均值


* rotate
* rotate
第9行: 第14行:
b=indgen(10)
b=indgen(10)
c=[rotate(a,1),rotate(b,10]
c=[rotate(a,1),rotate(b,10]
:转置
IDL>print,transpose([a,b])


* reverse
* reverse
第19行: 第26行:
IDL>print,fan(indgen(3),2)
IDL>print,fan(indgen(3),2)
IDL>print,fan(indgen(3),2,/trans)
IDL>print,fan(indgen(3),2,/trans)
可以用rebin实现
IDL>print,rebin(indgen(3),3,2)


* 多维数组的位置
* 多维数组的位置
第30行: 第39行:
IDL prints:
IDL prints:
Value at [3, 6] is 0.973381
Value at [3, 6] is 0.973381

:数组的位数,
print,size(array)
:size 还可以用来确定变量的类型 Ctype=size(A,/type),


* 数组随机打乱(shuffle)
* 数组随机打乱(shuffle)
Arr_S=arr[sort(randomu(seed,N_elements(arr))]
Arr_S=arr[sort(randomu(seed,N_elements(arr))]


*数组去重
==结构==
Arr=Arr[uniq(Arr,sort(arr))

*结构很容易增加一个tag,比如
A= {tag1:0.,tag2:'Name'}
B=create_struct(A,'tag3',1L)
但是如果A是结构数组了,就不能再这样操作。

结果数组经常被用来读入数据文件,如果该数据文件加上一列数据,如何较方便的操作。
:一种方法如下:
A= {tag1:0.,tag2:'Name'}
B= {tag1:1.,tag2:'Name2',tag3:10L}
A2= replicate(A,10)
B2 = replicate(B,10)
struct_assign,A,B ;把A的数值付给B
print,B ;可以看到B的前两列已经变成A的数值,但B的第三列被充0,所以这时候可以再给B.tag3赋值。


*数组求和
:还有一种方法,但没上面那么直观
a=total(indgen(10),/cum);输出为数组的累计分布
str={a: 0l, b: ''}
str_arr=REPLICATE(str,10)
ref_str = reform_struct(str_arr, /tag_array, 10)
ref_str = create_struct(ref_str, 'c' ,make_array(10))
help, ref_str, /str


==数组和矩阵运算==
还有一共jjadd_tag的程序(http://astro.berkeley.edu/~johnjohn/idl.html#JJADD_TAG)
*For A # B, where A and B are vectors, IDL performs A # TRANSPOSE(B). In this case, C = A # B is a matrix with Cij = Ai Bj. Mathematically, this is equivalent to the outer product, usually denoted by A Å B.
IDL> struct = {a: 0., b: 'blah'}
*For A ## B, where A and B are vectors, IDL performs TRANSPOSE(A) ## B. In this case, C = A ## B is a matrix with Cij = Bi Aj.
IDL> a=replicate(struct,2)
*To compute the dot product, usually denoted by A • B, use TRANSPOSE(A) # B.
IDL> c=jjadd_tag(a,'new',0b)
*A # B = B ## A
*A # B = (BT # AT)T ;T表示转置运算

2017年1月9日 (一) 04:37的最新版本

数组

  • REFORM 数组变形
将数组变为制定的维数
 A=indgen(20), B=reform(A,2,10) 
reform在默认(无输入参数)的情况下将数组变为一维
 A=reform(data[1,*])
  • 中位值

a=median(A,/even);偶数位的数据,取中间两个数的平均值

  • rotate
可以把数组旋转后组成矩阵,类似于列操作。
a=indgen(10)
b=indgen(10)
c=[rotate(a,1),rotate(b,10]
转置
IDL>print,transpose([a,b])
  • reverse
数组反向
例:数组降序排列,结合sort(默认升序)
PRINT, 'Elements of A in descending order: ', A[REVERSE(SORT(A))]
  • 扩维
fan程序(http://astro.berkeley.edu/~johnjohn/idl.html#FAN )
IDL>print,fan(indgen(3),2)
IDL>print,fan(indgen(3),2,/trans)

可以用rebin实现

IDL>print,rebin(indgen(3),3,2)
  • 多维数组的位置
用array_indices
 array = RANDOMU(seed, 10, 10)  
 mx = MAX(array, location) 
 ind=array_indices(array,location)
 print, ind, array[ind[0],ind[1]], format = '(%"Value at [%d, %d] is %f")'  
 IDL prints:
 Value at [3, 6] is 0.973381 
数组的位数,
print,size(array) 
size 还可以用来确定变量的类型 Ctype=size(A,/type),
  • 数组随机打乱(shuffle)
Arr_S=arr[sort(randomu(seed,N_elements(arr))]
  • 数组去重
Arr=Arr[uniq(Arr,sort(arr))
  • 数组求和
a=total(indgen(10),/cum);输出为数组的累计分布

数组和矩阵运算

  • For A # B, where A and B are vectors, IDL performs A # TRANSPOSE(B). In this case, C = A # B is a matrix with Cij = Ai Bj. Mathematically, this is equivalent to the outer product, usually denoted by A Å B.
  • For A ## B, where A and B are vectors, IDL performs TRANSPOSE(A) ## B. In this case, C = A ## B is a matrix with Cij = Bi Aj.
  • To compute the dot product, usually denoted by A • B, use TRANSPOSE(A) # B.
  • A # B = B ## A
  • A # B = (BT # AT)T ;T表示转置运算