【问题标题】:IDL equivalent of MATLAB function accumarray()IDL 等效于 MATLAB 函数 accumarray()
【发布时间】:2013-05-31 18:48:13
【问题描述】:

我接到了将一段 MATLAB 代码翻译成 IDL 的任务,并且 当我遇到 MATLAB 函数 accumarry() 时遇到了障碍。这 功能,描述here 用于根据另一个数组中给出的索引对一个数组中的元素求和。例子 1 可能比顶部的实际功能描述更好地解释了这一点 的页面。在尝试在 IDL 中重现示例 1 时,我无法避免 for 循环,但我相信这是可能的。我的最佳尝试如下:

vals = [101,102,103,104,105]
subs = [0,1,3,1,3]

n = max(subs)+1
accum = make_array(n)

for i = 0, n-1 do begin
   wVals = where(subs eq i,count)
   accum[i] = count eq 0 ? 0 : total(vals[wVals])
endfor

print,accum
;       101.000      206.000      0.00000      208.000

任何关于改进这一点的建议将不胜感激!我希望 IDL 有一个类似的内置功能,但一直没能找到。也许直方图分箱有什么魔力?

【问题讨论】:

    标签: matlab idl-programming-language


    【解决方案1】:

    我在 Coyote 的 IDL 网站上找到了许多可能的解决方案(不足为奇。) http://www.idlcoyote.com/code_tips/drizzling.html

    我最终使用了以下内容,作为性能和可读性之间的折衷:

    function accumarray,data,subs
    
    mx = max(subs)
    accum = fltarr(mx+1) 
    
    h = histogram(subs,reverse_indices=ri,OMIN=om)
    for j=0L,n_elements(h)-1 do if ri[j+1] gt ri[j] then $
       accum[j+om] = total(vals[ri[ri[j]:ri[j+1]-1]])
    
    return,accum
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      • 2013-03-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多