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

조회 수: 2 (최근 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)
plot(x,y,'r')
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 CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by