(* given a matrix mat_ with $n$ vectors of $m$ attributes, it creates a matrix with $n$ vectors and their first $k$ most 'important' attributes (ie., the K-L expansions of these $n$ vectors) *) KLexpansion[ mat_, k_:2] := mat . Transpose[ KL[mat, k] ]; (* given a matrix with $n$ vectors of $m$ dimensions, computes the first $k$ singular vectors, ie., the axes of the first $k$ Karhunen-Loeve expansion *) KL[ mat_ , k_:2 ]:= Module[ {n,m, avgvec, newmat,i, val, vec }, {n,m} = Dimensions[mat]; avgvec = Apply[ Plus, mat] / n //N; (* translate vectors, so the mean is zero *) newmat = Table[ mat[[i]] - avgvec , {i,1,n} ]; {val, vec} = Eigensystem[ Transpose[newmat] . newmat ]; vec[[ Range[1,k] ]] ]