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

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
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

댓글을 달려면 로그인하십시오.

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)

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

태그

질문:

2021년 9월 7일

답변:

2021년 9월 8일

Community Treasure Hunt

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

Start Hunting!

Translated by