How to plot 3 vectors but align them so that they are all originating from the same point?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Alex Vasin
il 14 Mag 2017
Commentato: Star Strider
il 15 Mag 2017
example
a = [-2 3]
b = [3 7]
c = [-3 5]
Thanks
3 Commenti
Risposta accettata
Star Strider
il 14 Mag 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 —
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/176191/image.png)
2 Commenti
Star Strider
il 15 Mag 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.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!