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
Related
How can I plot two polygons in one graph in maple?
I currently have:
display(polygon([[0, 0], [2, 0], [1,1]],
color = red),
scaling = constrained);
But this only plots one polygon, in this case it is a triangle, but i want to have two triangles next to each other?
You could define the polygons first and then display them:
g1 := polygon([[0, 0], [2, 0], [1,1]], color = red);
g2 := polygon([[0, 0], [4, 0], [1,1]], color = blue);
display(g1, g2);
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:
I have two vectors:
x = [1, 2, 3, 5, 6, 10, 20, 50, 100]
and
y=[7, 1, 2, 4, 2, 1, 5, 1, 1];
I am interested to plot Y as a function of X in MATLAB. So, this can be done by:
figure;
plot(x, y, '--rs');
In fact, the code above plots the curve, but the x-axis seems to be spaced as follows: 0, 10, 20, ..., 100.
What I want is to draw a curve which exhibits just the values in x as x-axis and their corresponding y-axis values. An example of x-axis is shown in the picture below.
Any help will be very appreciated!!
Instead of plotting using plot(x,y), use plot(1:numel(x),y) and use the XTick and XTickLabels` properties to change the labelling of the graph to suit your need.
Example:
clc
clear
x = [1, 2, 3, 5, 6, 10, 20, 50, 100];
y=[7, 1, 2, 4, 2, 1, 5, 1, 1];
plot(1:numel(x),y,'--rs')
set(gca, 'XTick', 1:length(x)); %// Change x-axis ticks
set(gca, 'XTickLabel', x); %// Change x-axis ticks labels.
Result:
Hope that helps!
I have three vectors in matlab:
x=[2, 3, 2, 3, 3]
y=[1, 5, 1, 5, 5]
Q=[7, 8, 4, 6, 8]
The modified vectors should be
x=[2, 3]
y=[1, 5]
Q=[12, 22 ]
Here x,y represents coordinates and Q a value depending upon (x,y).
The coordinates are getting repeated, like (2,1) has come twice, then I need to modify the vectors x and y representing unique coordinates and summing the values of Q for the particular coordinates e.g. (2,1) has come twice and the values of Q at those coordinates are 7 and 5 then for modified vectors coordinate (2,1) has to come once and corresponding Q value 7+5=12 and similarly for (3,5) Q is 8+6+8=22.
[XY, ~, ic] = unique([x' y'],'rows')
xu = XY(:,1).'; % The unique x you want
yu = XY(:,2).'; % The unique y you want
Qu = accumarray(ic,Q').';
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