You could consider either using broadcasting here, `X=X.-means`, or writing out the loop in both dimensions to avoid temporaries.