Effacer les filtres
Effacer les filtres

How can I isolate a set of data points on a graph using conditionals?

3 vues (au cours des 30 derniers jours)
Howard Griggs
Howard Griggs le 16 Fév 2024
Modifié(e) : Howard Griggs le 17 Fév 2024
I have the following sets of values, that when plotted, creates a simple linear graph (slope = 1). I would like to isolate the y values that are, for example, greater than or equal to 5 and plot them, while the other values would be set equal to zero. Therefore I would have a graph of the y values that are greater than or equal to 5 and y values that are equal to 0. For instance, in the code below, at x = 6, y = 6 and at x = 4, y = 4. I would want the new graph to set y = 0 at x =4 while keeping the y = 6 value.
In this specific case I know I could probably just index out the values I need, however, the data I am working with is not always the same. In my other attemps at solving this issue I tried using for loops to cycle through the data, but I could not figure out how to do it right. Any help would be greatly appreciated.
x = (1:1:10)
y = (1:1:10)
plot(x,y,'r')
grid on

Réponses (1)

Les Beckham
Les Beckham le 16 Fév 2024
The data doesn't need to be "linear" to use indexing to select the data that you want. For example:
x = 1:10;
y = rand(1, 10) * 10 % <<< random data between 1 and 10
y = 1×10
8.6063 6.1841 4.6272 1.3827 5.4659 0.3952 7.9899 4.2663 4.6411 8.2559
y_selected = y;
y_selected(y<5) = 0; % set any data less than 5 to 0 for plotting
plot(x, y_selected, '.-')
grid on

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by