Plotting matrices that don't have the same size

I am trying to plot two matrices it doesn't work.
matrix_1 is a 181x1 matrix with different values, I am trying to plot these values and also get a line fitted on the same plot, I tried polyfit as well but it doesn't work.
Message diplayed:
"Error using plot
Vectors must be the same length."
x=1941:2010
y=matrix_1
plot(x,y)

3 件のコメント

Matt J
Matt J 2018 年 11 月 29 日
How would it work? A plot, by definition, is a display of coordinate pairs (xi,yi). If your xi and yi data do not exist in corresponding pairs, what does it mean to "plot" them?
Bob Thompson
Bob Thompson 2018 年 11 月 29 日
編集済み: Bob Thompson 2018 年 11 月 29 日
Yes, matlab is expecting coordinate pairs.
How is your data related? X appears to be years, but should there be one set of results for each year, or is there some other correlation?
Samy Ben Thabet
Samy Ben Thabet 2018 年 11 月 29 日
Oh right my bad. I guess I made a mistake somewhere in my code I will try to fix that.
Thank you!

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

回答 (1 件)

Ken Atwell
Ken Atwell 2018 年 11 月 30 日

0 投票

When calling plot, the length of the x,y pairs must be equal. Here is an example of a plot of x^2 with noise added, then then a polyfit to that data:
x=-3:.1:3;
y=x.^2+rand(size(x));
f=polyfit(x, y, 2); % Returns coefficients, not y values directly
fy = f(1).*x.^2 + f(2).*x + f(3);
plot(x,y,x,fy)

1 件のコメント

Akira Agata
Akira Agata 2018 年 11 月 30 日
編集済み: Akira Agata 2018 年 11 月 30 日
If these two matrices are not the same size but matrix_1 is a sampled data from x = 1941 to 2010 with some fixed sampling frequency, adjusting length of x with linspace function would be possible solution. Here is an example.
x = 1941:2010;
y = matrix_1;
xInterp = linspace(min(x),max(x),numel(y));
figure
plot(xInterp,y)

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

カテゴリ

ヘルプ センター および File ExchangeLine Plots についてさらに検索

タグ

質問済み:

2018 年 11 月 29 日

編集済み:

2018 年 11 月 30 日

Community Treasure Hunt

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

Start Hunting!

Translated by