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

7 ビュー (過去 30 日間)
Howard Griggs
Howard Griggs 2024 年 2 月 16 日
編集済み: Howard Griggs 2024 年 2 月 17 日
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)
grid on

回答 (1 件)

Les Beckham
Les Beckham 2024 年 2 月 16 日
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


Help Center および File ExchangeGraph and Network Algorithms についてさらに検索




Community Treasure Hunt

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

Start Hunting!

Translated by