Scatter3, Meshgrid coordinate misalignment. How to fix this?

5 views (last 30 days)
Hello,
I have a 3d matrix I want to graph on scatter3. But there's something that's concerning me about the way Matlab parses the meshgrid outputs:
>> a = rand(2, 3, 4)
a(:,:,1) =
0.5557 0.2120 0.9138
0.1844 0.0773 0.7067
a(:,:,2) =
0.5578 0.1662 0.9879
0.3134 0.6225 0.1704
a(:,:,3) =
0.2578 0.0740 0.4024
0.3968 0.6841 0.9828
a(:,:,4) =
0.4022 0.1544 0.1611
0.6207 0.3813 0.7581
>> [x, y, z] = meshgrid(1:2, 1:3, 1:4);
>> [x(:) y(:) z(:) a(:)]
ans =
1.0000 1.0000 1.0000 0.5557
1.0000 2.0000 1.0000 0.1844
1.0000 3.0000 1.0000 0.2120
2.0000 1.0000 1.0000 0.0773
2.0000 2.0000 1.0000 0.9138
2.0000 3.0000 1.0000 0.7067
1.0000 1.0000 2.0000 0.5578
1.0000 2.0000 2.0000 0.3134
1.0000 3.0000 2.0000 0.1662
2.0000 1.0000 2.0000 0.6225
2.0000 2.0000 2.0000 0.9879
2.0000 3.0000 2.0000 0.1704
1.0000 1.0000 3.0000 0.2578
1.0000 2.0000 3.0000 0.3968
1.0000 3.0000 3.0000 0.0740
2.0000 1.0000 3.0000 0.6841
2.0000 2.0000 3.0000 0.4024
2.0000 3.0000 3.0000 0.9828
1.0000 1.0000 4.0000 0.4022
1.0000 2.0000 4.0000 0.6207
1.0000 3.0000 4.0000 0.1544
2.0000 1.0000 4.0000 0.3813
2.0000 2.0000 4.0000 0.1611
2.0000 3.0000 4.0000 0.7581
Say x is 1:2, y is 1:3, z is 1:4, but if you look at the final output, the second value 0.1844 has coordinates (1, 2, 1) when it actually has (2, 1, 1), as you can see from the first part. It looks like the alignment is wrong in this approach.
How would I fix this?
Thank you

Accepted Answer

Matt J
Matt J on 6 Jan 2013
Use NDGRID instead of MESHGRID.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!