d = pdist2(first_xyz, second_xyz);
[mindist, idx] = min(d, , 2);
Here first_xyz and second_xyz should be something-by-3 arrays of x, y, z coordinates.
The idx that results will have as many rows as first_xyz has, and the value will be the index of which second_xyz row is closest to that point; the mindist value will tell you how close it is (no more computation will be required.)
If you want the index for each n (second dataset) as to which m it is closest to, then use min(d, , 1)