added permutation of dimesions and then re-permuted them so MATLAB would work