a*******r 发帖数: 7558 | 1 【 以下文字转载自 Computation 讨论区 】
发信人: alligator (侏罗纪的幸存者), 信区: Computation
标 题: IDL一问
发信站: BBS 未名空间站 (Mon Apr 30 22:10:00 2007)
我需要做些IDL的操作,例如
result=b1*0.0 & ix=where(b1 NE 0.0)
result(ix)=b2(ix)/b1(ix)
现在想弄个loop,对一批数据做上述处理:
b3(ix)/b1(ix), b4/b1,.... bn/b1
如果是igor之类的,可以很容易用类似于
String/G b_n="b"+num2str(n)
variable n=1
do
result=$band_name_n/b1
n+=1
while(...)
有谁知道 IDL 里该怎样进行此类循环? 我翻了IDL手册但没有找到
对应的number_to_string 函数,虽然google到一个num2str.pro | n**l 发帖数: 38 | 2 string()
【在 a*******r 的大作中提到】 : 【 以下文字转载自 Computation 讨论区 】 : 发信人: alligator (侏罗纪的幸存者), 信区: Computation : 标 题: IDL一问 : 发信站: BBS 未名空间站 (Mon Apr 30 22:10:00 2007) : 我需要做些IDL的操作,例如 : result=b1*0.0 & ix=where(b1 NE 0.0) : result(ix)=b2(ix)/b1(ix) : 现在想弄个loop,对一批数据做上述处理: : b3(ix)/b1(ix), b4/b1,.... bn/b1 : 如果是igor之类的,可以很容易用类似于
| G*******s 发帖数: 76 | 3 IDL里面为了效率,一般避免用loop,lz的计算貌似可以避免loop
【在 a*******r 的大作中提到】 : 【 以下文字转载自 Computation 讨论区 】 : 发信人: alligator (侏罗纪的幸存者), 信区: Computation : 标 题: IDL一问 : 发信站: BBS 未名空间站 (Mon Apr 30 22:10:00 2007) : 我需要做些IDL的操作,例如 : result=b1*0.0 & ix=where(b1 NE 0.0) : result(ix)=b2(ix)/b1(ix) : 现在想弄个loop,对一批数据做上述处理: : b3(ix)/b1(ix), b4/b1,.... bn/b1 : 如果是igor之类的,可以很容易用类似于
| a*******r 发帖数: 7558 | 4 how to do bn/b1 for n=1,2,.. w/o loop?
【在 G*******s 的大作中提到】 : IDL里面为了效率,一般避免用loop,lz的计算貌似可以避免loop
| a*******r 发帖数: 7558 | 5 3x
my data array are b1, b2, b3,...b100...
To do operations like b2/b1, b3/b1,...., I first do this:
for i=1, n do begin
my_data='b'+strtrim(string(i),1)
endfor
Now the string my_data has become b1, b2,..., but how to
convert the string my_data to the actual data b1, b2...?
【在 n**l 的大作中提到】 : string()
| G*******s 发帖数: 76 | 6 suppose your Bi(b1, b2...) is a vector(not necessarily a vector though), and
the number of elements in each vector are the same, then you can
store all data to a 2-dim array by the following,
dim b_all[n_elements(b1), number_of_BIs] as your_data_type
b_all = [b1,b2... bn] ; if n is large, do a loop
now you can do whatever operation you need
btw,you may use temporary(Bi) to improve efficiency and save memory
【在 a*******r 的大作中提到】 : 3x : my data array are b1, b2, b3,...b100... : To do operations like b2/b1, b3/b1,...., I first do this: : for i=1, n do begin : my_data='b'+strtrim(string(i),1) : endfor : Now the string my_data has become b1, b2,..., but how to : convert the string my_data to the actual data b1, b2...?
| a*******r 发帖数: 7558 | 7 Thank you very much!
and
【在 G*******s 的大作中提到】 : suppose your Bi(b1, b2...) is a vector(not necessarily a vector though), and : the number of elements in each vector are the same, then you can : store all data to a 2-dim array by the following, : dim b_all[n_elements(b1), number_of_BIs] as your_data_type : b_all = [b1,b2... bn] ; if n is large, do a loop : now you can do whatever operation you need : btw,you may use temporary(Bi) to improve efficiency and save memory
| G*******s 发帖数: 76 | 8 my previous post is a workaround by avoiding the problem. however,
it could be solved in IDL.
first, please let me know weather i have got your question correctly. To
rephrase your question, 'how to access a variable with its string name?'am i
right?
if yes, one of the solutions is to use execute() function. check online
help
plz.
【在 a*******r 的大作中提到】 : Thank you very much! : : and
| a*******r 发帖数: 7558 | 9 let's say:
>b1=[1., 2., 3.]
>a='b'
>c=1
>d=a+num2str(c)
>print, d
b1
>e='print,b[1]'
>r=execute(e)
2.0000
I need to use string d (b1) to call array b1, how can I achieve
this in IDL?
i
【在 G*******s 的大作中提到】 : my previous post is a workaround by avoiding the problem. however, : it could be solved in IDL. : first, please let me know weather i have got your question correctly. To : rephrase your question, 'how to access a variable with its string name?'am i : right? : if yes, one of the solutions is to use execute() function. check online : help : plz.
| G*******s 发帖数: 76 | 10 pro test
b1 = [1.,2.,3.]
for i = 0, n_elements(b1) -1 do begin
r = execute ( 'b_tmp = b1[' + strtrim(string(i),2) + ']' )
print, b_tmp
endfor
end
【在 a*******r 的大作中提到】 : let's say: : >b1=[1., 2., 3.] : >a='b' : >c=1 : >d=a+num2str(c) : >print, d : b1 : >e='print,b[1]' : >r=execute(e) : 2.0000
| G*******s 发帖数: 76 | 11 要是你的字符串'b1'中的'1'也是动态生成的,也是一样的。类似很多其他语言里的根据
变量名字符串做个宏替换得到真正的变量。
pro test
b1 = [1.,2.,3.]
for j = 1, 1 do begin
str_of_var = 'b' + strtrim(string(j),2)
endfor
for i = 0, n_elements(b1) -1 do begin
r = execute ( 'b_tmp = ' + str_of_var + '[' + strtrim(string(i),2) +
']' )
print, b_tmp
endfor
end
【在 G*******s 的大作中提到】 : pro test : b1 = [1.,2.,3.] : for i = 0, n_elements(b1) -1 do begin : r = execute ( 'b_tmp = b1[' + strtrim(string(i),2) + ']' ) : print, b_tmp : endfor : end
| a*******r 发帖数: 7558 | 12 呵呵,太谢谢了,回头试试。
根据
+
【在 G*******s 的大作中提到】 : 要是你的字符串'b1'中的'1'也是动态生成的,也是一样的。类似很多其他语言里的根据 : 变量名字符串做个宏替换得到真正的变量。 : pro test : b1 = [1.,2.,3.] : for j = 1, 1 do begin : str_of_var = 'b' + strtrim(string(j),2) : endfor : for i = 0, n_elements(b1) -1 do begin : r = execute ( 'b_tmp = ' + str_of_var + '[' + strtrim(string(i),2) + : ']' )
| a*******r 发帖数: 7558 | 13 总结一下两种解决办法。感谢Geomatics和James Jones的详尽回复。
1. Use SCOPE_VARFETCH, which can be compiled in a Runtime program, if
necessary.
The syntax "myArray[i] = scope_varfetch('var')" will copy the value of a
variable named "var" that is
defined in the current scope (scope is where you are running in the current
"call stack" - what routine
call you are currently running in) into 'myArray'.
2. Use EXECUTE, which cannot be compiled in a .sav Runtime program:
EXECUTE treats its string argument as a command that |
|