How mean values of skew diagonals of a $(n+1,n)$ matrix can be computed efficiently?
Here is my naive implementation:
ClearAll[build] ;
build[matrix_] := Block[
{col,row,signal},
{col,row} = Dimensions[matrix] ;
signal = ConstantArray[0,2*row] ;
Do[
signal[[i]] = Table[If[q+p==i+1,matrix[[q,p]],Nothing],{q,1,col},{p,1,row}] ;
signal[[i]] = Mean[Flatten[signal[[i]]]] ;
,{i,1,2*row,1}
] ;
signal
]
Looks like it's time complexity is $O(n^3)$, can it be reduced?
Example:
n = 4 ;
ncols = n + 1 ;
nrows = n ;
matrix = Array[m,{ncols,nrows}] ;
matrix
build[matrix]
(* {{m[1,1],m[1,2],m[1,3],m[1,4]},{m[2,1],m[2,2],m[2,3],m[2,4]},{m[3,1],m[3,2],m[3,3],m[3,4]},{m[4,1],m[4,2],m[4,3],m[4,4]},{m[5,1],m[5,2],m[5,3],m[5,4]}} *)
(* {m[1,1],1/2 (m[1,2]+m[2,1]),1/3 (m[1,3]+m[2,2]+m[3,1]),1/4 (m[1,4]+m[2,3]+m[3,2]+m[4,1]),1/4 (m[2,4]+m[3,3]+m[4,2]+m[5,1]),1/3 (m[3,4]+m[4,3]+m[5,2]),1/2 (m[4,4]+m[5,3]),m[5,4]} *)
n = 4 ;
ncols = n + 1 ;
nrows = n ;
data = Range[1,2*n] ;
data = Partition[data,n,1] ;
data
build[data]
(* {{1,2,3,4},{2,3,4,5},{3,4,5,6},{4,5,6,7},{5,6,7,8}} *)
(* {1,2,3,4,5,6,7,8} *)