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

2 Ansichten (letzte 30 Tage)
Howard Griggs
Howard Griggs am 16 Feb. 2024
Bearbeitet: Howard Griggs am 17 Feb. 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

Antworten (1)

Les Beckham
Les Beckham am 16 Feb. 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

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Produkte


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by