Attempting to Plot In A Specific Range
15 ビュー (過去 30 日間)
古いコメントを表示
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?
0 件のコメント
回答 (2 件)
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
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))
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!