multiple plots from array without loop

조회 수: 5 (최근 30일)
Tommy B
Tommy B 2016년 12월 2일
답변: Tommy B 2016년 12월 2일
Hi all
I have an array (X) holding lets say 20 cycles of data. I would like to plot the entire 20 cycles as a dashed line plot, and then markup (with a solid line) specific segments of the graph, the start and end indexes are in two different vectors S1 and S2. Would it be possible to do something like this:
plot(X,'k--');
hold on;
plot(X([S1:S2]),'r');
instead of using a loop as:
plot(X,'k--');
for i=1:length(S1)
plot(X(S1(i):S2(i),r,)
end
The only reason is I would like to avoid loops in my code, as I am plotting several graph segments within my code, and then having 30 for loops does not look good :)
Best, Tommy
  댓글 수: 2
KSSV
KSSV 2016년 12월 2일
This will work:
plot(X,'k--');
hold on;
plot(X([S1:S2]),'r');
You can try on your own...why doubt?
Tommy B
Tommy B 2016년 12월 2일
편집: Tommy B 2016년 12월 2일
Hi Well because I did try, and it did not work :/
X =[1 2 3 4 5 6 7 8 9 10];
S1=[1 4 9];
S2=[2 5 10];
plot(X,'k--');
hold on;
plot(X([S1:S2]),'r');
I want this to mark the section X= 1-2 & 4-5 & 9-10 with a red line, the rest should be dashed. With the above code it only marks the first segment in red.
Best, Tommy

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

채택된 답변

KSSV
KSSV 2016년 12월 2일
편집: KSSV 2016년 12월 2일
I understand now...You may follow some thing like this:
X =[1 2 3 4 5 6 7 8 9 10];
S1=[1 4 9];
S2=[2 5 10];
plot(X,'k--');
hold on;
Y = NaN(size(X)) ;
Y([S1 S2]) = X([S1 S2]) ;
plot(Y,'r');
Introduce NaN's at the place you want to skip...plot will not show NaN's.

추가 답변 (1개)

Tommy B
Tommy B 2016년 12월 2일
Yes, perfect! Thanks a lot!
Have a nice weekend.
Best, Tommy

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by