Mark specific values on a plot!

조회 수: 8 (최근 30일)
Panos Ale
Panos Ale 2017년 10월 19일
답변: Hellen Nassuna 2018년 10월 8일
Hi consider the following plot:
x=1:100;
y=600+(1400-600).*rand(100,1);
plot(x,y)
How can I mark the y values between 900 and 1100? Thanks!
  댓글 수: 1
Rik
Rik 2017년 10월 19일
With hold on and plotting a line over it with an increased line width you could emphasize a certain range.

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

채택된 답변

Birdman
Birdman 2017년 10월 19일
Additional to your code, use the following lines:
yMark=zeros(1,100);%preallocation for speed
for i=1:1:length(y)
if(y(i)>=900 && y(i)<=1100)
yMark(i)=y(i);
end
end
plot(x,yMark,'->')
  댓글 수: 2
Rik
Rik 2017년 10월 19일
You can vectorize the loop:
yMark=zeros(1,100);
yMark(y>=900 & y<=1100)=y(y>=900 & y<=1100)
Be aware that this code (both mine and the loop) will result in zero values outside the wanted range. You can fix this by either initializing to a NaN (just replace zeros with NaN), or shortening x the same way as you shorten y:
yMark=y(y>=900 & y<=1100)
xMark=x(y>=900 & y<=1100)
Birdman
Birdman 2017년 10월 19일
Yes you are right. It will make the code more efficient. I just wanted to mark those values on the plot. Thank you for your attention.

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

추가 답변 (1개)

Hellen Nassuna
Hellen Nassuna 2018년 10월 8일
x=1:100;
y=600+(1400-600).*rand(100,1);
plot(x,y)
hold on;
yMark=NaN(1,100);%preallocation for speed
for i=1:1:length(y)
if(y(i)>=900 && y(i)<=1100)
yMark(i)=y(i);
end
end
plot(x,yMark,'->')
hold off
if true
% code
end

카테고리

Help CenterFile Exchange에서 Graphics Performance에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by