Attempting to Plot In A Specific Range
46 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Voss
on 15 Jun 2022
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 Comments
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!