Attempting to Plot In A Specific Range

65 ビュー (過去 30 日間)
AnnieLieseMari
AnnieLieseMari 2022 年 6 月 15 日
回答済み: Voss 2022 年 6 月 15 日
Hello!
I am trying to plot some y-values in a specific range of x-values. I have x-values to -3.5 to 3.5 and should be increasing by one.
The y-values are all over the place, but I need all of them to be plotted. There are 45 values in the y vector.
I attempted to plot them using the following code...
x1 = [-3.5:3.5]
% y is a 45 x 1 single vector
plot(x1, y, '-')
However, when I do so, I get an error saying the vectors need to be the same length. What can I do to limit the y-vector to only contain values that fall in the x range?

回答 (2 件)

Torsten
Torsten 2022 年 6 月 15 日
編集済み: Torsten 2022 年 6 月 15 日
x1 = (linspace(-3.5,3.5,numel(y))).';
plot(x1,y,'-')

Voss
Voss 2022 年 6 月 15 日
You will need a vector, the same length as y, containing an x value for each y value.
Let's say you have that vector and it's called x.
% Let's say this is your vector of x values
% (increasing by 1)
x = -21.5:22.5
x = 1×45
-21.5000 -20.5000 -19.5000 -18.5000 -17.5000 -16.5000 -15.5000 -14.5000 -13.5000 -12.5000 -11.5000 -10.5000 -9.5000 -8.5000 -7.5000 -6.5000 -5.5000 -4.5000 -3.5000 -2.5000 -1.5000 -0.5000 0.5000 1.5000 2.5000 3.5000 4.5000 5.5000 6.5000 7.5000
Then you can plot the points (x- and y-coordinates) where x is between -3.5 and 3.5 like this:
% (make up some random y)
y = rand(1,numel(x));
% limit the plot to those values of x between -3.5 and 3.5
idx = x >= -3.5 & x <= 3.5; % or idx = abs(x) <= 3.5;
plot(x(idx),y(idx))

カテゴリ

Help Center および File ExchangeLine Plots についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by