How to neatly exclude values from a plot?

I'm plotting 2 functions: eta, feta. What I want to do is plot feta for values between 0, 0.5, 0.7, 0.8, 0.95 and exclude the rest.
I tried somethig like this:
plot(eta(eta<0.5 | eta>0.7), feta(eta<0.5 | eta>0.7), 'k')
but it hasn't worked. Any ideas?

답변 (1개)

Image Analyst
Image Analyst 2023년 11월 17일

0 개 추천

"What I want to do is plot feta for values between 0, 0.5, 0.7, 0.8, 0.95 and exclude the rest. "
So that means you want feta between 0 and 0.95 and nowhere else. So do this:
indexes = (feta > 0) & (feta < 0.95);
plot(eta(indexes), feta(indexes), 'k-', 'LineWidth', 2);
grid on;

댓글 수: 3

HC98
HC98 2023년 11월 17일
편집: HC98 2023년 11월 17일
ok great! Is there a way I can set values outside this range to nan so they aren't plotted and the lines don't join?? So basically it only plots eta when indexes = true
Dyuman Joshi
Dyuman Joshi 2023년 11월 17일
"Is there a way I can set values outside this range to nan"
Yes, use the indices to assign.
Image Analyst has already shown you how to get the indices, you can utilize that to assign the values.
What I showed you is how to plot only within the range. If you want to plot the whole range but have NaNs where you don't want to plot, you can do that too:
indexes = (feta > 0) & (feta < 0.95);
eta(indexes) = nan;
feta(indexes) = nan;
plot(eta, feta, 'k-', 'LineWidth', 2);
grid on;
Elements that are nan will not plot a marker (if you're using one) at the nan location, and will not have lines (if you're using them) connecting the nan point to adjacent non-nan points.
Not sure what the x axis limits will be in that case, so you might want to use xlim.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

제품

릴리스

R2023a

태그

질문:

2023년 11월 17일

댓글:

2023년 11월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by