how to plot the projection of a vector? - matlab

I have made the following program for calculating the vector projection:
a=[6 7]
b=[1 4]
p=(dot(a,b)/(b*b'))*b
the result of p is [2 8] that is the projection of a on b.
I read that for plotting a vector in Matlab I should choose some origin points, so I have added those to the vectors and form a set of matrices with them like this:
x=[0 0; 6 7]
y=[0 0; 1 4]
z=[0 0; 2 8]
plot3(x,y,z)
grid;
but I cannot get to visualize the projection, what am I missing?
Thanks

You can use quiver for 2D vector plotting or quiver3 for 3D plotting.
a = [6 7];
b = [1 4];
p = (dot(a,b)/dot(b,b))*b;
figure;
quiver(0,0,a(1), a(2));
hold on;
quiver(0,0,b(1), b(2));
quiver(0,0,p(1), p(2));

Related

MATLAB .fig to a mesh file?

I'm not sure if this is possible, but given the following in MATLAB:
a = [1 2 3]
b = [3 1 2]
G = Graph(a,b)
x = [0 0 1]
y = [0 1 0]
z = [1 0 0]
plot(G, 'XData', x, 'YData', y, 'ZData', z)
The plot can be outputted into a .fig file. The goal would be to turn this into any sort of mesh file, such that it outputs into just nodes and edges, but no surfaces (or create small, cylindrical surfaces as edges).

Plot data ignoring some X-axis values on Matlab

I'm trying to plot some data on Matlab using the following code:
x = [1 2 5 6 7 9]
y1 = [1 2 3 2 1 2]
y2 = [2 2 2 1 3 3]
y3 = [1 1 2 3 1 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
And I'm getting the following result:
Note that I have no Y values for the X positions 3, 4 and 8, but the X axis is still showing these X values in the graph.
There is some way that I can ignore the positions 3, 4 and 8 in the X-axis, and show only the Y values for the X positions 1, 2, 5, 6, 7 and 9?
I can use the following command to 'hide' these positions:
set(gca, 'XTick', x);
But the gaps related to these positions are still there.
Update:
This is the graph I'm trying to create (it was created on paint):
Note: in my case, the X-axis just represents the IDs of some images, and because of that I just need to show the numbers.
You can get the plot you want by first leaving x out in the calls to plot (so it plots against the array index) then altering the XTick and XTickLabel properties of the axes (and adjusting the x limit slightly):
plot(y1,'--.','markersize',20); hold on;
plot(y2,'--.','markersize',20);
plot(y3,'--.','markersize',20);
legend('y1','y2','y3');
xlim([1 numel(x)]); % Note numel is used here
ylim([0 4]);
set(gca, 'XTick', 1:numel(x), 'XTickLabel', cellstr(num2str(x(:))));
I am not sure if you need gaps between plot lines. If you wish no values to be plotted there, try replacing the values by nan. For example,
x = [1 2 3 4 5 6 7 8 9]
y1 = [1 2 nan nan 3 2 1 nan 2]
y2 = [2 2 nan nan 2 1 3 nan 3]
y3 = [1 1 nan nan 2 3 1 nan 1]
plot(x,y1,'--.','markersize',20); hold on;
plot(x,y2,'--.','markersize',20); hold on;
plot(x,y3,'--.','markersize',20); hold off;
legend('y1','y2','y3');
xlim([1 9]);
ylim([0 4]);
This will give a plot like this:
Sounds like you need stem.
stem(x, y1);
legend('y1');

plot vectors of data

I have three vectors of data; first column is axis x, second column axis y and third column is the measured data point value v:
x = [1 2 3 4 5];
y = [2 3 4 5 6];
v = [0 -1 +2 3 -5];
Is there a way to plot this in matlab and color the data points according to their value? I tried with scatter, but that doesn't give color coding.
Just use scatter, it can accept values for each point. Just set the size of each point, set it to be filled, turn on the colorbar and that's it. Just please don't use the jet colormap...
x = [1 2 3 4 5];
y = [2 3 4 5 6];
v = [0 -1 +2 3 -5];
pt_sz = 30;
colormap parula
scatter(x, y, pt_sz, v, 'filled');
colorbar;
grid on
You can try pcolor. It works like
z=rand(4,4);
pcolor(R,C,z);
Where z is a matrix storing the data (your v), R is the row (your x) and C is the column (your y).

How to create a surface plot on a multiple line 3d plot?

Using plot3 I have created a multiple discrete line 3d plot. How can I fill the 3d surface these lines create (basically join the lines with a surface)?
Using fill3 can help.
x = [0 0 5 5 0];
y = [5 5 0 0 5];
z = [0 5 5 0 0];
subplot(1,2,1)
plot3(x,y,z,'LineWidth',5);
grid on
subplot(1,2,2)
fill3(x,y,z,'y')
grid on

Add markers to a step function in Matlab

I have the following step function:
I wish to add 2 markers, on the left hand side of the horizontal line as a filled circle and on the right hand side a circle which is not filled with color. How can I do this please? Thank you.
I used very simple code for this graph. The code is:
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
x=[1.5 2.6];
y=[2 2];
plot(x, y)
hold on
x=[2.6 5];
y=[3 3];
plot(x, y)
hold on
x=[5 6];
y=[4 4];
plot(x, y)
hold on
x=[6 6.5];
y=[5 5];
plot(x, y)
hold on
x=[6.5 8];
y=[6 6];
plot(x, y)
axis([0 9 0 7])
Do as follows for each segment (the new lines are the last two):
x=[1 1.5];
y=[1 1];
plot(x,y)
hold on
plot(x(1),y(1),'o','MarkerFaceColor','b') %// filled circle
plot(x(2),y(2),'o') %// non-filled circle