Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Average over the columns of the matrix in Julia

Tags:

matrix

julia

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.
like image 666
D. Sukhov Avatar asked Dec 05 '25 16:12

D. Sukhov


1 Answers

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
like image 132
Nils Gudat Avatar answered Dec 08 '25 19:12

Nils Gudat