Scatter problem3

This is probably scatter3

what I don't understand. I have a matrix from which all slices but the last are NaNed ( M(:,:,1:10) = NaN;

) and then it moved the first and last dimension. Thus, M(11,:,:)

there are only meanings in. I expect all plotted values ​​to be in the YZ plane at a point x==11

, but the graph looks different (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);

      

enter image description here

+3


source to share


1 answer


Explained what meshgrid

toggles x and y:

From the meshgrid

documentation:

MESHGRID is similar to NDGRID, except that the order of the first two input and output arguments are toggled (ie, [X, Y, Z] = MESHGRID (x, y, z) gives the same result as [Y, X , Z] = NDGRID (y, x, z)).



At first glance, this should result in a plot with values ​​in the XZ plane at y == 11 (i.e. x and y are swapped compared to what you originally expected). But note that your code does not handle x and y correctly (because of meshgrid

). This has the additional effect that the coordinates x

and y

"shuffled," and you do not see the plane (even in the XZ), but rather a lattice.

So the solution is to use ndgrid

that doesn't perform any switches. Just replace " meshgrid

" with " ndgrid

" in your code. The resulting figure is now as expected:

enter image description here

+2


source







All Articles