Reputation: 7592
Probably it's scatter3
what I don't understand. I have a matrix of which all slices but the last are NaNed (M(:,:,1:10) = NaN;
) and then it's permuted switching first and last dimension. So there are only values in M(11,:,:)
. I expect all plotted values to be in the Y-Z plane at x==11
, but the plot looks differently (see code and picture below). Any explanations?
M = rand(22,55,11);
M(:,:,1:10) = NaN;
M = permute(M,[3 2 1]);
shape = size(M)
[x, y, z] = meshgrid(1:shape(1), 1:shape(2), 1:shape(3));
scatter3(x(:), y(:), z(:), 4, M(:), 'fill');
view([60 60]);
xlabel('X ', 'FontSize', 16);
ylabel('Y ', 'FontSize', 16);
zlabel('Z ', 'FontSize', 16);
Upvotes: 3
Views: 91
Reputation: 112669
The explanation is that meshgrid
switches x and y:
From meshgrid
documentation:
MESHGRID is like NDGRID except that the order of the first two input and output arguments are switched (i.e., [X,Y,Z] = MESHGRID(x,y,z) produces the same result as [Y,X,Z] = NDGRID(y,x,z)).
At first sight, this should result in a plot with values in the X-Z plane at y==11 (i.e. x and y interchanged with respect to what you initially expected). But note that your code treats x and y sizes incorrectly (because of meshgrid
). This has the additional effect that the x
and y
coordinates get "shuffled" and you don't see a plane (even in X-Z), but rather a lattice.
So the solution is to use ndgrid
, which doesn't do any switching. Just replace "meshgrid
" by "ndgrid
" in your code. The resulting figure is now as expected:
Upvotes: 2