MatLab - understanding input of plot3 - matlab

I have a brief question regarding the plot3 function in MatLab.
Say I write the following:
x = [1 1 -1 1];
y = [4 4 4 4];
z = [-1 1 1 -1];
plot3(x,y,z)
Why does MatLab then draw a triangle with vertices in (-1,4-1), (1, 4, -1) and (1,4,1) instead of just plotting the points (-1, 4, -1), (1, 4, -1) and (1, 4, 1)?

The default line style for plot and plot3 is a line, not just points. If you want to plot just the points, use a different style:
plot3(x,y,z,'or'); #% plots red circles at each point

Related

How can I create a rectangle with a hole in MATLAB/OCTAVE?

I would like to plot/draw exactly this shape in MATLAB or OCTAVE. Of course I do know how to plot, and how to create rectangles, using either the plot, the line or the rectangle functions. But I have not yet managed to add this "hole" on the top side of the rectangle. I figure, it's a (half-)circle of radius 0.5 and center point (1.5|2). In OCTAVE, there is a drawCircleArc function, but I don't want to only draw that thing, but also to have the necessary coordinates defining the whole shape for further manipulation.
Here is one way (matlab/octave compatible):
% Specify all polygon points, excluding the semi-circle outline
X = [1, 0, 0, 3, 3, 2];
Y = [2, 2, 0, 0, 2, 2];
% Add semi-circle outline to array of polygon points
t = 0 : -0.01 : -pi;
X = [X, 1.5 + 0.5 * cos(t)];
Y = [Y, 2 + 0.5 * sin(t)];
% Use fill to plot the filled polygon, with desired settings
fill( X, Y, [0.8, 0.8, 0.8], 'linewidth', 1.5 );
axis( [-2, 4, -2, 4] ); axis equal;
As of 2017b you can also use polyshapes and boolean operators.
rect = polyshape([0 3 3 0], [0 0 2 2]);
t = linspace(0, 2*pi, 32);
circ = polyshape(1.5+.5*cos(t), 2+.5*sin(t));
subplot 121, hold on
plot(rect)
plot(circ)
axis equal
shape = subtract(rect, circ);
subplot 122
plot(shape)
axis equal

Polygon Rotation using matrix-vector multiplication

Consider the polygon with vertices (0, 0), (1, 0), (7/10, 1), (1/2, 1/2), and (3/10, 1). Make a plot of this polygon in Matlab using the fill function. Rotate this polygon by an angle of 100 degrees using matrix-vector multiplication in Matlab with an appropriately chosen rotation matrix R. Make another plot of the rotated polygon using fill.
% Makes original polygon
X = [0 1 7/10 1/2 3/10];
Y = [0 0 1 1/2 1];
norm_poly = fill(X,Y,'k');
thetad = 100;
R = [cosd(thetad) -sind(thetad); sind(thetad) cosd(thetad)];
C = repmat([0 0], 5, 1)';
axis([-10 10 -10 10])
V = get(norm_poly,'Vertices')'; % get the current set of vertices
V = R*(V - C) + C; % do the rotation relative to the centre of the
square
set(norm_poly,'Vertices',V'); % update the vertices
How would I make to different plots to show them? Does the code to rotate make sense and answer all the requirements?
The rotation itself makes sense. To plot multiple things to the same graph use hold on after making the first plot. Alternatively you can make a new figure and plot a new fill there.
P = [0, 1, 7/10, 1/2, 3/10;
0, 0, 1, 1/2, 1];
fill(P(1,:), P(2,:), 'k');
theta = 100;
R = #(t) [cosd(t) -sind(t); sind(t) cosd(t)];
axis([-3 3 -3 3])
grid on
V = R(theta)*P;
hold on;
fill(V(1,:), V(2,:), 'k');

Data Plot in Matlab

I have two arrays say X and Y with same dimension. I can plot each points (x,y) by plot(X,Y). But how can I color them according to their given labels?
Say X = [3, 4, 2, 5, 6], Y = [2, 2, 1, 5, 6] and label = [1, 2, 2, [1,2], 2]. Here I all have to do is to color points with label=1 with blue and points in label=2 by red. How can I do this?
There are several ways to optimize this code and even get away without using the loop but this should get you started
for i=1:length(X)
xdot=X(i)
ydot=Y(i)
Ldot=label(i)
col=[1 0 0;0 0 1];
plot(xdot,ydot,'color',col(Ldot,:),'marker','o');
hold on
end

How do I plot a triangle in three dimensions? [duplicate]

This question already has answers here:
How can I plot a 3D-plane in Matlab?
(4 answers)
Closed 8 years ago.
I would like to draw different triangles by using MATLAB shown in the figure below. Suppose that I have 3 vectors V1=[1 1 1], V2=[-1 1 1], v3=[-2 -2 -2].
How can I draw triangle with these vectors in 3D?
![enter image description here][1]
You can use plot3() like this:
v1=[1 1 1]; v2=[-1 1 1]; v3=[-2 -2 -2];
triangle = [v1(:), v2(:), v3(:), v1(:)];
plot3(triangle(1, :), triangle(2, :), triangle(3, :))
xlabel('x'); ylabel('y'); zlabel('z');
This is the output:
Edit:
This is in order to plot axis:
val = 5; %// Max value of axis
axX = [0 0 0; val 0 0];
axY = [0 0 0; 0 val 0];
axZ = [0 0 0; 0 0 val];
plot3(axX(:, 1), axX(:, 2), axX(:, 3), 'k');
plot3(axY(:, 1), axY(:, 2), axY(:, 3), 'k');
plot3(axZ(:, 1), axZ(:, 2), axZ(:, 3), 'k');
text(val, 0, 0, 'x')
text(0, val, 0, 'y')
text(0, 0, val, 'z')
view(3)
In addition you can make the plot look like your reference image, adding these commands to above code:
set(gca,'xtick',[], 'xcolor', 'w')
set(gca,'ytick',[], 'ycolor', 'w', 'YDir','reverse')
set(gca,'ztick',[], 'zcolor', 'w')
view(45, 30)
This is the result:

matlab plot the matrix with custom colors

Is there a way to specify the colors of the lines when plotting the matrix.
For instance:
// here is my matrix A
A = [13, 3, 4;19, 0, 1;18, 0, 2;19, 0, 1;19, 0, 1];
// when I am plotting it I am not in control of what color each line will be
plot(A)
Using
plot(A, 'r')
just colors everything in red (which is expected)
When trying something like
plot(A, ['r', 'g','b'])
or
plot(A, 'rgb')
does not work (which is not surprising)
So is there any way to specify color for each line?
You can change the color afterwards:
A = [13 3 4;
19 0 1;
18 0 2;
19 0 1;
19 0 1];
p=plot(A);
clrs = jet(numel(p)); % just a Nx3 array of RGB values
for ii=1:numel(p)
set(p(ii),'color',clrs(ii,:));
end
Example:
A=sin(repmat(linspace(0,2*pi,200),20,1)'*diag(linspace(1,2,20)));
% same thing as above
The plot function doesn't provide a way to do it as concisely as in your example. Instead, you can try:
plot(A(:, 1), 'r', A(:, 2), 'g', A(:, 3), 'b');