Attempting to Plot In A Specific Range
25 vues (au cours des 30 derniers jours)
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?
0 commentaires
Réponses (2)
Voss
le 15 Juin 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 commentaires
Voir également
Catégories
En savoir plus sur Line Plots dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
