3D matrix transpose

Hi everyone,

How to transpose 3d matrix?
I just do it in the following way:

global void transpose3D(float* inputMatrix, float* outputMatrix, int nx, int ny, int nz)
{
int px = blockDim.x * blockIdx.x + threadIdx.x;
int py = blockDim.y * blockIdx.y + threadIdx.y;
int pz = blockIdx.z * blockDim.z + threadIdx.z;

if  (px<nx && py<ny && pz<nz)
{
    outputMatrix[px*ny*nz + py*nz + pz] = inputMatrix[pz*nx + py*nx*nz + px];
}

}

Is it correct?

Thanks a lot!