How to plot a 3D matrix with plot3

1.6k views Asked by At

I have an image(mat file) and can access it as a 2D matrix 2*10095. I then made linear algebra work on it to project it onto a plane with normal vector N =[1,2,3]and got the values for the 3D matrix (x,y,z).

How can I plot the 3D matrix with plot3 in Matlab? plot3 takes three arguments (x,y,z) and each column in my matrix has those arguments [x;y;z] but how can I pass all the 10095 columns to plot3 - wouldn't that show me the image "laying" down accordingly to my calculation on the z axis?

I have used this to plot the image when it was as 2D matrix:

plot(T2(1, :), T2(2, :), 'k.','MarkerSize', 1);
1

There are 1 answers

0
MichaelTr7 On

If the data is setup in this configuration:

Bottom layer (first layer) → x component/value (:,:,1)

Middle layer (second layer) → y component/value (:,:,2)

Top layer (third layer) → z component/value (:,:,3)

For a smooth/interpolated plot the function surf() can be used alternatively.

3D Scatter Plot

%Random test data%
Matrix = zeros(2,10095,3);
Matrix(:,:,1) = randn(2,10095);
Matrix(:,:,2) = randn(2,10095);
Matrix(:,:,3) = randn(2,10095);

%Grabbing each x,y,z coordinate of the matrix%
X = Matrix(:,:,1);
Y = Matrix(:,:,2);
Z = Matrix(:,:,3);

plot3(X,Y,Z,'o','color','b');
grid;

Using MATLAB version: R2019b