On Thursday, September 18, 2014 2:36:53 PM UTC-5, paul analyst wrote:
>
> No idea ?
>
> W dniu 2014-09-18 o 13:26, paul analyst pisze:
> > I have a vector x = int (randbool (100))
> > a / how quickly (without the loop?) receive 10 vectors of length 10,
> > in each field the average of the next 10 fields of the vector x
> > (moving/stepen average of 10 values at step = 10)?
> > b / how to receive the 99 vectors of length 10 of the next 10 fields
> > wektra x (moving/stepen average of 10 values at step = 1)?
> >
> > Paul
I'm not sure what the first result you want is and what you mean by
"without the loop". A general moving average of m vector positions with
steps of 1 could be written
function movingavg1(v::Vector,m::Int)
n = length(v)
0 < m < n || error("m = $m must be in the range [1,length(v)] =
[1,$(length(v))]")
res = Array(typeof(v[1]/n), n-m+1)
s = zero(eltype(res))
for i in 1:m
s += v[i]
end
res[1] = s
for j in 1:(length(res)-1)
s -= v[j]
s += v[j + m]
res[j+1] = s
end
res/m
end
To test this
julia> vv = int(randbool(100));
julia> show(vv)
[0,0,0,1,1,1,1,1,1,0,1,1,0,0,1,1,1,1,0,1,0,0,0,1,0,1,1,0,1,1,1,1,1,0,0,1,1,0,1,0,0,0,0,1,1,0,1,0,0,0,0,0,1,0,1,0,0,0,0,1,0,0,0,1,0,0,1,1,0,1,0,1,1,1,1,0,1,0,0,1,1,1,0,0,1,1,0,0,1,1,0,0,1,0,0,0,1,0,1,1]
julia> show(movingavg1(vv,10))
[0.6,0.7,0.8,0.8,0.7,0.7,0.7,0.7,0.7,0.6,0.7,0.6,0.5,0.5,0.6,0.5,0.5,0.5,0.4,0.5,0.5,0.6,0.7,0.8,0.7,0.7,0.7,0.7,0.7,0.7,0.6,0.5,0.4,0.3,0.4,0.5,0.4,0.4,0.4,0.3,0.3,0.3,0.3,0.4,0.3,0.3,0.3,0.2,0.2,0.2,0.3,0.3,0.3,0.2,0.3,0.2,0.2,0.3,0.4,0.4,0.4,0.4,0.5,0.6,0.6,0.7,0.7,0.7,0.6,0.6,0.6,0.7,0.7,0.6,0.5,0.5,0.6,0.5,0.5,0.6,0.6,0.5,0.4,0.5,0.5,0.4,0.3,0.4,0.4,0.4,0.4]
julia> mean(vv[1:10])
0.6
julia> mean(vv[2:11])
0.7
julia> mean(vv[91:100])
0.4
If you are always working with Bool values you are probably better off
returning the moving sums than the moving averages.