How to plot 3 vectors but align them so that they are all originating from the same point?

2 vues (au cours des 30 derniers jours)
example
a = [-2 3]
b = [3 7]
c = [-3 5]
Thanks
  3 commentaires
Jan
Jan le 14 Mai 2017
Modifié(e) : Jan le 14 Mai 2017
What is "plotv"? How do you want the "vectors" to appear? All you provide are the location of 3 points in 2D.
Alex Vasin
Alex Vasin le 14 Mai 2017
I want 3 vectors all to start from the same point and go outwards. Apparently that's what plotv is supposed to do, but it only seems to allow for 2 vectors.
i.e. vectors all have direction, start point and end point. I want the plot to have all 3 vectors to have their start positions to be in the same point on the graph.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 14 Mai 2017
This may do what you want. You will have to set the ‘startm’ row number in the repmat call to match the number of vectors you want to plot (here, 3). This code works for three vectors.
The Code
startv = [1 1]; % Starting Point
startm = repmat(startv, 3, 1);
a = [-2 3];
b = [3 7];
c = [-3 5];
abc = [a; b; c];
figure(1)
plot([startm(:,1) abc(:,1)]', [startm(:,2) abc(:,2)]')
grid
axis([-5 5 0 10])
The Plot
  2 commentaires
Alex Vasin
Alex Vasin le 15 Mai 2017
Thank you. I don't understand any of it, but I will use it anyway.
Star Strider
Star Strider le 15 Mai 2017
My pleasure.
I apologise for not explaining how it works.
The ‘startv’ is the (1x2) vector defining the start point for all the vectors. The ‘startm’ matrix duplicates it to form a (3x2) matrix to match the number of vectors (here 3) that you want to plot. (This is necessary for the plot arguments to agree.)
The ‘abc’ matrix is a vertical concatenation of the three vectors you defined. Creating these matrices makes the code easier to write.
The plot arguments themselves require a bit of explaining. The plot function requires a vector of points for each (x,y) pair (line plotted), so the x vectors for each are the x coordinate of your start point horizontally concatenated with the x coordinate of ‘abc’. The y vectors are defined similarly from the y coordinate of the start point and the y coordinates of ‘abc’. This creates two (3x2) matrices as arguments to plot. So it plots from the x value of the start point and the x value of the ‘abc’ vectors to the y values of the start point and ‘abc’ vectors, defined similarly.
Through the magic of the way plot works, this plots the individual lines.
If you have any further questions about it, post back here and I will do my best to explain it, and if necessary change it if you want the lines plotted differently.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by