在循环中定义数组名称

发布于 2024-09-12 21:24:12 字数 426 浏览 2 评论 0原文

我可能会以错误的方式处理这个问题,但我正在尝试在循环内定义和填充数组。

for i = 0,39 do begin

xx = long(findgen(n+1l)*sx + line1x[i]) 
sz = size(xx)
arrayname = 'line' + strtrim(i,2)
arrayname = findgen(3,sz[1])
arrayname[0,*] = xx
arrayname[1,*] = yy
arrayname[2,*] = vertline

endfor

这显然行不通,但是有没有办法使用 'line' + strtrim(i,2) 定义的字符串在每次迭代时创建并填充一个新数组?在本例中,我有 40 个名称为 line0...39 的数组。这里困难的部分是 sz[1] 变化,所以我不能简单地定义一个大数组来容纳所有内容。

I may be going about this the wrong way, but I'm trying define and fill arrays within a loop.

for i = 0,39 do begin

xx = long(findgen(n+1l)*sx + line1x[i]) 
sz = size(xx)
arrayname = 'line' + strtrim(i,2)
arrayname = findgen(3,sz[1])
arrayname[0,*] = xx
arrayname[1,*] = yy
arrayname[2,*] = vertline

endfor

This obviously won't work, but is there a way to use the string defined by 'line' + strtrim(i,2) to create and fill a new array upon each iteration? In this case I'd have 40 arrays with names line0...39. The difficult part here is that sz[1] varies, so I can't simply define one large array to hold everything.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-09-19 21:24:12

在 idl 8.0 或更高版本中,您可以使用 HASH 数据类型来实现此目的。

您的代码如下所示:

array_dict = hash()
for ii = 0,39 do begin
  xx = long(findgen(n+1l)*sx + line1x[ii]) 
  sz = size(xx)
  arrayname = 'line' + string(1, FORMAT='(i02)')
  array = findgen(3,sz[1])
  array[0,*] = xx
  array[1,*] = yy
  array[2,*] = vertline

  array_dict[arrayname] = array
endfor

您现在可以按名称访问数组:

line = array_dict['line01']

In idl 8.0 or later you could use the HASH datatype for this.

Your code would looks like this:

array_dict = hash()
for ii = 0,39 do begin
  xx = long(findgen(n+1l)*sx + line1x[ii]) 
  sz = size(xx)
  arrayname = 'line' + string(1, FORMAT='(i02)')
  array = findgen(3,sz[1])
  array[0,*] = xx
  array[1,*] = yy
  array[2,*] = vertline

  array_dict[arrayname] = array
endfor

You can now access your arrays by name:

line = array_dict['line01']
郁金香雨 2024-09-19 21:24:12

好吧,如果您想要进行肮脏的黑客攻击(并且不需要它在未经许可的虚拟机安装上运行),那么总是有 execute 函数。

但是您是否考虑过声明一个一维指针数组,其中每个元素都指向 3 个 by sz 子数组之一?这给你带来了一个大数组的一些好处,
没有所有子数组必须具有相同形状的约束。
它可能看起来像这样......

ptrs=ptrarray(40) ; Allocate an array of 40 pointers, initialized to null

for i = 0,39 do begin
  ; calculate sz, xx, yy, vertline
  dummy=findgen(3,sz[1])
  dummy[0,*] = xx
  dummy[1,*] = yy
  dummy[2,*] = vertline
  ptrs[i]=ptr_new(dummy) ; create copy of dummy on the heap, storing pointer in ptrs[i]

endfor

; To access the i-th subarray...

(*ptrs[i])[0,*] = new_xx
(*ptrs[i])[1,*] = new_yy
(*ptrs[i])[2,*] = new_vertline

Well, there's always the execute function, if you're in the mood for a filthy hack (and don't need it to run on an unlicensed virtual machine installation).

But have you considered declaring a 1-D array of pointers, where each element points to one of your 3 by sz subarrays? That gives you some of the benefit of one big array,
without the constraint of all the subarrays having to have the same shape.
It might look something like this...

ptrs=ptrarray(40) ; Allocate an array of 40 pointers, initialized to null

for i = 0,39 do begin
  ; calculate sz, xx, yy, vertline
  dummy=findgen(3,sz[1])
  dummy[0,*] = xx
  dummy[1,*] = yy
  dummy[2,*] = vertline
  ptrs[i]=ptr_new(dummy) ; create copy of dummy on the heap, storing pointer in ptrs[i]

endfor

; To access the i-th subarray...

(*ptrs[i])[0,*] = new_xx
(*ptrs[i])[1,*] = new_yy
(*ptrs[i])[2,*] = new_vertline
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文