朱莉娅矩阵的列的平均值

发布于 2025-02-03 01:40:56 字数 487 浏览 0 评论 0原文

我有一个带有表单的浮点条目的大矩阵,

[ a b c d 
  e f g h
  i j k l
  m n o p ]

其中一些值是离群值,因此我想平均每个条目,其值及其在相应列中的近期k条目并保留形状。换句话说,对于k = 3:

[        a                  b                   c                  d 
      (e + a)/2          (f + b)/2          (g + c)/2          (h + d)/2
    (e + a + i)/3      (f + b + j)/3      (g + c + k)/3      (h + d + l)/3
    (e + i + m)/3      (f + j + n)/3      (g + k + o)/3      (h + l + p)/3   ] 


 etc.

I have a big matrix with float entries of the form

[ a b c d 
  e f g h
  i j k l
  m n o p ]

some of the values are outliers, so I wanted to average each of the entries with values with its recent k entries in the corresponding column and preserve the shape. In other words to have something like this for k = 3:

[        a                  b                   c                  d 
      (e + a)/2          (f + b)/2          (g + c)/2          (h + d)/2
    (e + a + i)/3      (f + b + j)/3      (g + c + k)/3      (h + d + l)/3
    (e + i + m)/3      (f + j + n)/3      (g + k + o)/3      (h + l + p)/3   ] 


 etc.

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

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

发布评论

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

评论(2

就此别过 2025-02-10 01:40:56

您可以使用rollingfunctions映射

julia> a = reshape(1:16, 4, 4)
4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64:
 1  5   9  13
 2  6  10  14
 3  7  11  15
 4  8  12  16

julia> using RollingFunctions

julia> mapslices(x -> runmean(x, 3), a, dims = 1)
4×4 Matrix{Float64}:
 1.0  5.0   9.0  13.0
 1.5  5.5   9.5  13.5
 2.0  6.0  10.0  14.0
 3.0  7.0  11.0  15.0

You can do this with RollingFunctions and mapslices:

julia> a = reshape(1:16, 4, 4)
4×4 reshape(::UnitRange{Int64}, 4, 4) with eltype Int64:
 1  5   9  13
 2  6  10  14
 3  7  11  15
 4  8  12  16

julia> using RollingFunctions

julia> mapslices(x -> runmean(x, 3), a, dims = 1)
4×4 Matrix{Float64}:
 1.0  5.0   9.0  13.0
 1.5  5.5   9.5  13.5
 2.0  6.0  10.0  14.0
 3.0  7.0  11.0  15.0
巾帼英雄 2025-02-10 01:40:56

我不知道rollingfunctions,但是常规循环速度更快4倍。我不确定这是否是由maplices

function runmean(a,W)
    A = similar(a)
    for j in axes(A,2), i in axes(A,1)
        l = max(1, i-W+1)
        A[i,j] = mean(a[k,j] for k=l:i)
    end
    A 
end

测试产量:

using RollingFunctions 
@btime mapslices(x -> runmean(x, 3), A, dims = 1) setup=(A = rand(0.0:9,1000,1000))
@btime runmean(A,3) setup=(A = rand(0.0:9,1000,1000))

  15.326 ms (10498 allocations: 23.45 MiB)
   4.410 ms (2 allocations: 7.63 MiB)

I didn't know about RollingFunctions, but a regular loop is 4X faster. I'm not sure if it's some kind of type instability caused by mapslices?.

function runmean(a,W)
    A = similar(a)
    for j in axes(A,2), i in axes(A,1)
        l = max(1, i-W+1)
        A[i,j] = mean(a[k,j] for k=l:i)
    end
    A 
end

Testing yields:

using RollingFunctions 
@btime mapslices(x -> runmean(x, 3), A, dims = 1) setup=(A = rand(0.0:9,1000,1000))
@btime runmean(A,3) setup=(A = rand(0.0:9,1000,1000))

  15.326 ms (10498 allocations: 23.45 MiB)
   4.410 ms (2 allocations: 7.63 MiB)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文