image pixel relocation
===

I have a nxmx3 matrix where the current locations relate to the source the image came from. I'm trying to map the image to another source. The image is of size nxm and I have the spacial x and y positions of the pixel in the first and second layer respectively, and the intensity value of the pixel in the 3rd layer. I need to somehow reorganize the pixels and display them according to the spatial locations stored in the first two layers. 

This means that in img(i,j,:) i have the x position, the y position, and the gray scale pixel value.

Is there any way to go about doing this?