How can I isolate a set of data points on a graph using conditionals?
6 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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_selected = y;
y_selected(y<5) = 0; % set any data less than 5 to 0 for plotting
plot(x, y_selected, '.-')
grid on
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Line Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!