[beginner] How do you plot two vectors of diffrent length?

1 次查看(过去 30 天)
Hi,
How do you go about to plot two vectors of diffent lengths in Matlab? One of my homework assignments is to great a plot graph using these two vectors:
x1 = [1 2 3 4 5 6]
y1 = [3,5 4,8 7,1 9,1 10,5 13,2]
However, this seems impossible due to the nature of the plot function, where it demands vectors to be of same length. I tried plot(x1, y1) and that didn't work. We haven't gotten anymore information than this :(
Maybe the teacher inserted a comma when it should've been a decimal point inside vector y1? He's Swedish, so maybe that's why.

回答(2 个)

Dave B
Dave B 2021-9-7
编辑:Dave B 2021-9-7
At some level the question is what you expect from the plot.
When I look at your y1 vector, I notice that it's got an odd arrangement of commas and spaces. I wondered if maybe it was intending to make a 2x6 or 6x2 matrix, which would make a lot of sense to plot against the numbers 1 to 6.
x1 = [1 2 3 4 5 6];
y1 = [3 5;4 8;7 1;9 1;10 5;13 2];
plot(x1,y1)
More generally, to answer the title, you can plot two different length vectors as follows:
y1 = rand(1,5);
y2 = randn(1,7);
plot(y1)
hold on
plot(y2)
  2 个评论
Emanuel Strid
Emanuel Strid 2021-9-7
编辑:Emanuel Strid 2021-9-7
Thank you so much! A matrix makes much more sense! I guess my teacher incorrectly arranged the y1 vector with commas. I was just perplexed as he had never mentioned a syntax like that
Dave B
Dave B 2021-9-7
Happy to help, sometimes writing code involves a certain amount of mind-reading.
But just to be clear, the semicolons are the really critical bit, commas and spaces are the same when specifying a matrix (but I like the way it looks without the commas:
y1 = [3 5;4 8;7 1;9 1;10 5;13 2];
y2 = [3,5;4,8;7,1;9,1;10,5;13,2];
isequal(y1,y2)
ans = logical
1

请先登录,再进行评论。


Steven Lord
Steven Lord 2021-9-8
I believe your suspicion about whether the commas should be periods is correct. MATLAB uses the convention that the decimal separator is the period, but another convention is to use a comma.
x1 = [1 2 3 4 5 6];
y1 = [3.5 4.8 7.1 9.1 10.5 13.2];
plot(x1, y1)

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by