문제

How do I calculate the average vector of n adjacent numbers in a vector in APL?

Consider the vector: a← 2 3 4 5 6

Input: 2 should return 2.5 3.5 4.5 5.5,

3 should return 3 4 5,

4 should return 3.5 4.5.

avg←(+/a)÷⍴a won't help in this case!

도움이 되었습니까?

해결책

Use n-wise reduction to get the sums, then divide:

f←{(⍺+/⍵)÷⍺}
      2 f 1 2 3 4 5
1.5 2.5 3.5 4.5
      3 f 1 2 3 4 5
2 3 4
      4 f 1 2 3 4 5
2.5 3.5

Oops, now with your exact inputs:

f←{(⍺+/⍵)÷⍺}
       2 f 2 3 4 5 6
2.5 3.5 4.5 5.5
       3 f 2 3 4 5 6
3 4 5
       4 f 2 3 4 5 6
3.5 4.5
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top