The problem is that cumsum(mapslices(V,X,1)) generates a row vector, while (1:n) is interpreted as a column vector. The ./ generates a 1e6 x 1e6 dense matrix. What you want is
avgX = cumsum(mapslices(V, X, 1))./((1:n)')
HOWEVER, this is still much slower than your second version, which is counter-intuitive for me. Maybe somebody else can explain.