Attempting to Plot In A Specific Range
Afficher commentaires plus anciens
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?
Réponses (2)
x1 = (linspace(-3.5,3.5,numel(y))).';
plot(x1,y,'-')
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))
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
