Ploting two different vectors in the same figure

Hi, I have two vectors v1=[0; -1/sqrt(2);1/sqrt(2)]; and v2=[0; 1/sqrt(2); -1/sqrt(2)]; which I am trying to plot on the same figure, with diffrent colors and the end and start points with stars (*). But when I do this and click "run" I get an error "Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'." Is there a simpler way of writing this?
v1=[0; -1/sqrt(2);1/sqrt(2)];
t=linspace(0,1,100);
a = v1*t;
v2=[0; 1/sqrt(2); -1/sqrt(2)];
t=linspace(0,1,100);
b = v2*t;
plot3(a(:,1),a(:,2),a(:,3),'lineWidth',2,'Marker','*', 'r' );
hold on
plot3(b(:,1),b(:,2),b(:,3),'lineWidth',2,'Marker','*', 'g');

 採用された回答

Star Strider
Star Strider 2019 年 11 月 17 日

0 投票

Use element-wise multiplication, then transpose ‘a’ and ‘b’ to use the references in your plot3 calls. Alos, specifically use the 'Color' name-value pair.
Try this:
v1=[0; -1/sqrt(2);1/sqrt(2)];
t=linspace(0,1,100);
a = (v1.*t).';
v2=[0; 1/sqrt(2); -1/sqrt(2)];
t=linspace(0,1,100);
b = (v2.*t).';
plot3(a(:,1),a(:,2),a(:,3),'lineWidth',2,'Marker','*', 'Color','r' );
hold on
plot3(b(:,1),b(:,2),b(:,3),'lineWidth',2,'Marker','*', 'Color','g');
hold off
grid on
I added grid on to give a reference in the plot.

4 件のコメント

Jenny Andersen
Jenny Andersen 2019 年 11 月 17 日
編集済み: Jenny Andersen 2019 年 11 月 17 日
Thank you very much! But do you know why the stars do not appear in the figure?
Star Strider
Star Strider 2019 年 11 月 17 日
As always, my pleasure!
The stars do appear. They are just packed together in the line.
To have them only appear at the ends of the lines, the plot3 calls need to specify that:
plot3(a(:,1),a(:,2),a(:,3),'lineWidth',2, 'Color','r' );
hold on
plot3(b(:,1),b(:,2),b(:,3),'lineWidth',2,'Color','g');
plot3(a([1 end],1),a([1 end],2),a([1 end],3),'lineWidth',2, 'Marker','*', 'Color','r' );
plot3(b([1 end],1),b([1 end],2),b([1 end],3),'lineWidth',2, 'Marker','*', 'Color','g');
hold off
Note that to plot them onnly at the ends, use [1 end] in the first (row) argument of your matrices. That specifies two points only, one at the beginning and the other at the end of each vector.
That should do what you want. (The rest of your code is unchanged.)
Jenny Andersen
Jenny Andersen 2019 年 11 月 17 日
Thank you again :)
Star Strider
Star Strider 2019 年 11 月 17 日
As always, my pleasure!
(I apologise for originally overlooking that you said you wanted the stars at the ends of the lines in your original Question.)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by