I have the following code to plot some points on a 2-D x,y coordinate plane:
A=[1, 1; 1, 5; 3, 9; 4, 2; 4, 6; 6, 2; 7, 6; 6, 9; 9, 9];
figure
plot(A(:,1),A(:,2),'r.','LineWidth',2,'MarkerSize',10);
axis([0 10 0 10]);
str=num2str(A);
text(A(:,1)*1.02,A(:,2)*1.02,str);
However, my problem is I will like to link each point to every other point, to form a mesh.
Can anyone please assist with this problem.
You can double loop through the rows of A and draw a line between each pair. If you add these lines to the end of your code:
hold on
for ii = 1:(size(A, 1) - 1)
for jj = (ii + 1):size(A, 1)
line([A(ii, 1), A(jj, 1)], [A(ii, 2), A(jj, 2)])
end
end
You will get the plot with each point connected to all the other points:
Related
I am trying to generate the same plot as the one shown below,
, and I have written the following piece of code in MATLAB,
x1 = [0, 0, 1, 1];
x2 = [0, 1, 0, 1];
Y = [4, 3, 2, 4];
plot3(x1,x2,Y,'o','Color','r','MarkerSize',10, 'MarkerFaceColor', 'r')
hold on
[x y] = meshgrid(0:0.1:2, 0:0.1:2);
z = 1.25 + 2.5 * x + 1.5 * y;
surf(x, y, z,'FaceAlpha',0.5)
xlabel('x1');ylabel('x2');zlabel('y');
hold off
grid on
Would you please tell me how I can plot a line that connects the data point with its corresponding value of the generated surface for the same plot I generated with MATLAB.
Many thanks for your time.
I added a loop within the "hold" portion of your code.
x1 = [0, 0, 1, 1];
x2 = [0, 1, 0, 1];
Y = [4, 3, 2, 4];
plot3(x1,x2,Y,'o','Color','r','MarkerSize',10, 'MarkerFaceColor', 'r')
hold on
[x y] = meshgrid(0:0.1:2, 0:0.1:2);
z = 1.25 + 2.5 * x + 1.5 * y;
surf(x, y, z,'FaceAlpha',0.5)
xlabel('x1');ylabel('x2');zlabel('y');
for ii=1:numel(Y)
plot3( [x1(ii),x1(ii)], [x2(ii),x2(ii)], [Y(ii),1.25+2.5*x1(ii)+1.5*x2(ii)] );
end
hold off
grid on
Also, currently your surface z is hard-coded, therefore the code for the vertical line is hard-coded as well. But once you estimate your parameters for the regression, you could just as easily dynamically adjust the surface and the vertical lines.
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
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:
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');
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