问题描述
例如,
我的文件在 00.dat,01.dat,02.dat ...之后命名,每个文件包含多列,我使用READCOL
将它们读入变量.
My files are naming after 00.dat, 01.dat, 02.dat..., each file contains multiple columns and I use READCOL
to read them into variables.
for i = 0, n-1 do begin
readcol, string(i, F='(I02)')+'.dat', F='D,D', a0, b0
readcol, string(i, F='(I02)')+'.dat', F='D,D', a1, b1
.
.
c1 = a1 / a0
c2 = a2 / a0
.
.
d1 = b1 / b0
d2 = b2 / b0
.
.
endfor
这很好用,但是如果有100个变量,我不能一一键入所有的varialbes.
This works fine, but I cannot type all the varialbes one by one if there will be, say, one hundred variables.
因此,我想使用for循环生成: a(i),b(i),c(i),d(i).从这种意义上讲,代码将如下所示:
Therefore, I want to use for loop to generate: a(i), b(i), c(i), d(i). In that sense, the code will look like:
for i = 0, n-1 do begin
readcol, string(i, F='(I02)')+'.dat',F='D,D', a(i), b(i)
endfor
for i = 0, n-1 do begin
c(i) = a(i) / a(0)
d(i) = b(i) / b(0)
endfor
但这是行不通的,有什么方法可以在for循环中以及在做数学时声明变量吗?
But this doesn't work, is there any method to declare variables in a for loop and while doing math?
(我不是英语母语人士.如果我的问题中有任何不清楚之处,请告诉我.谢谢!)
推荐答案
很高兴在StackOverflow上看到另一个IDL程序员!
Nice to see another IDL programmer on StackOverflow!
我认为部分问题是READCOL
期望使用简单的变量名它的输出,在第二个示例中,您给它数组表达式,例如a(i)
和b(i)
而不是a
和b
.
I think part of the problem is that READCOL
is expecting simple variable names forits outputs, and in your second example you're giving it array expressions likea(i)
and b(i)
instead of a
and b
.
如果我正确理解了您的问题,则想替换一维数组第一个示例中的a0
,a1
,b0
,b1
等,带有二维数组a
,b
,等等,其中每个数组都有维度(nfiles,samples_per_file).所以如果你知道预先从每个文件读取多少行,您可以执行以下操作:
If I understand your question correctly, you want to replace the 1-dimensional arraysa0
, a1
, b0
, b1
, etc. from your first example, with 2-dimensional arrays a
, b
,etc. where each array has dimensions (nfiles, samples_per_file). So if you knowin advance how many lines will be read from each file, you could do something like this:
a=dblarr(n,samples_per_file)
b=dblarr(n,samples_per_file)
; similarly for c, d, etc.
for i = 0, n-1 do begin
readcol, string(i, F='(I02)')+'.dat',F='D,D', x, y
a[i,*] = x
b[i,*] = y
c[i,*] = x/x[0]
d[i,*] = y/y[0]
endfor
此版本传递READCOL
所需的简单变量名称,然后将其复制放入二维变量的子数组中.
This version passes READCOL
the simple variable names it's expecting, then copies theminto subarrays of the 2-D variables.
如果您事先不知道每个文件中有多少个样本,则可以分配第一次循环迭代期间的二维数组:
If you don't know in advance how many samples are in each file, you could allocatethe 2-d arrays during the first loop iteration:
for i = 0, n-1 do begin
readcol, string(i, F='(I02)')+'.dat',F='D,D', x, y
if (i EQ 0) then begin
samples_per_file = n_elements(x)
a = dblarr(n, samples_per_file)
b = dblarr(n, samples_per_file)
c = dblarr(n, samples_per_file)
d = dblarr(n, samples_per_file)
endif
a[i,*] = x
b[i,*] = y
c[i,*] = x/x[0]
d[i,*] = y/y[0]
endfor
当然,这全部假设每个文件包含相同数量的样本.如果不,您可能需要将a
,b
,c
和d
更改为一维指针数组,然后在读取文件时使用PTR_NEW
为每个文件的数据分配内存.
Of course, this all assumes that each file contains the same number of samples. If not,you'd probably need to change a
, b
, c
, and d
to 1-dimensional arrays of pointers,then use PTR_NEW
to allocate memory for each file's data as you read it.
(请注意,我使用方括号[]
表示法进行数组索引,发现比a(i)
,b(i)
等更容易阅读,后者可能与函数调用混淆.)
(Note that I've used the square bracket []
notation for array indexing, which I find abit easier to read than a(i)
, b(i)
etc. which can be confused with function calls.)
这篇关于如何在for循环中声明变量? (IDL)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!