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

1 view (last 30 days)
Emanuel Strid on 7 Sep 2021
Answered: Steven Lord on 8 Sep 2021
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.

Dave B on 7 Sep 2021
Edited: Dave B on 7 Sep 2021
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) Dave B on 7 Sep 2021
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 on 8 Sep 2021
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) 