MATLAB Answers

Having a trouble combining two plots using hold and plot3

1 view (last 30 days)
Haraldur Blöndal Kristjánsson
Answered: Srivardhan Gadila on 31 Oct 2020
I have an image in .mat file and can access the matrix that cointains the x,y coordinates for the it as can be seen in the comments in my code. Then I have to project it onto a plane in R³ and plot it and include in the plot a position vector. I have done all this but I am having two troubles with my plot
1. I want the pixles to be black not blue in
plot3(X,Y,Z,'o','color','b');%the image
2. This is my projected image when I only use the plot above (not hold on, add the postion vector plot, hold off). Its the way I want it, except the mentioned color problem.
Here is how the plot becomes when I add the position vector to it - why does it go to 2D
data = load('batman.mat');
%the batman image represented by a2-by-10095-matrix where the first row represent
%the x-coordinates, the second the y -coordinatesof all the pixels which are black in the image.
R = data.B; %Matrix with 10095 coordinates [x;y]
%Project onto the plane with normal N = (1,2,3)
%Here I change the given 2D matrix to 3D by adding the third row of zeros
%for Z
col_size = size(R, 2);
ones_row = zeros(1, col_size);
R = [R; ones_row];
P_null = [0; 0; 0;] %point of origin, the normal vector goes through
N = [1,2,3] %Normal vector
%Equation of the plane is then 1x + 2y 2 3z = dot(P_null,N) - which is 0
%Then transpose(x,y.t) = ((x,y, (1x + 2y)/-3))
A = [1 0 ; 0 1; -1/3 -2/3]; %get this matrix with two spanning vectors from above calculation
%Method of least squares
ATA = transpose(A) * A;
ATB = transpose(A) * R; %R is the origninal batman matrix, whith extra 0 row for z coordinates
x_hat = linsolve(ATA,ATB);
Final = A * x_hat
R
%cordinates for the image
X = Final(1,:,:);
Y = Final(2,:,:);
Z = Final(3,:,:);
%coordinates for the position vector
x = 10*linspace(0, 1, 100);
y = 10*linspace(0, 2, 100);
z = 10*linspace(0, 3, 100);
hold on
plot3(x, y, z, 'k-') %position vector
plot3(X,Y,Z,'o','color','b'); %the image
grid;
hold off
This is an example of what I am seeking after (just with a grid visible):

  0 Comments

Sign in to comment.

Answers (1)

Srivardhan Gadila
Srivardhan Gadila on 31 Oct 2020
In order to get black pixels change the color argument from 'b' to 'k' (refer to LineSpec)
plot3(X,Y,Z,'o','color','k')
In order to get both of them in same plot change the lines:
hold on
plot3(x, y, z, 'k-') %position vector
plot3(X,Y,Z,'o','color','b'); %the image
grid;
hold off
to
plot3(x, y, z, 'k-') %position vector
hold on
plot3(X,Y,Z,'o','color','k'); %the image
grid;
hold off
The following is an example code:
t = 0:pi/500:pi;
xt1 = sin(t).*cos(10*t);
yt1 = sin(t).*sin(10*t);
zt1 = cos(t)*0;
x = t-pi/2; y = t-pi/2; z = t-pi/2;
plot3(xt1,yt1,zt1,'o','color','k')
hold on
plot3(x, y, z, 'k-')
grid
Refer to the documentation of plot3, hold.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!