필터 지우기
필터 지우기

How to plot over certain range using if statements?

조회 수: 5 (최근 30일)
DrWooo
DrWooo 2017년 11월 13일
댓글: DrWooo 2017년 11월 14일
Hello,
I've inputted data from an excel file (defined here as B), and would like to generate two separate plots -- one where the value in column 7 is <25, and a second where the value in column 7 is >= 25.
So far, the code is:
s = B(1:18,7);
for x = B(1:18,5)
for y = B(1:18,12);
for i=1:length(B(1:18,7));
if s(i)<25;
figure(1);
subplot(3,1,1);
scatter(x,y); grid on;
hold on
elseif s(i)>=25;
figure(2);
subplot(3,1,1);
scatter(x,y); grid on;
hold on
end
end
end
end
The plots are identical, and it appears that the code is checking that the value in column is < or >= 25, and then plotting all of (x,y). How do I limit it so it only plots (x,y) under those conditions separately?
Any advice or suggestions are appreciated.
Cheers

채택된 답변

KSSV
KSSV 2017년 11월 14일
x = rand(100,1) ;
y = rand(100,1) ;
idx1 = y >= 0.5 ;
idx2 = y < 0.5 ;
figure
hold on
scatter(x(idx1),y(idx1),50,y(idx1),'s','filled')
scatter(x(idx2),y(idx2),50,y(idx2),'c','filled')
legend('data1','data2')
  댓글 수: 1
DrWooo
DrWooo 2017년 11월 14일
Thanks for the response. I was unfamiliar with the id function, and it's working great. The new code is:
for x = B(1:18,5)
for y = B(1:18,12);
idx1 = B(1:18,7) < 25; %Decided to remove the s variable
idx2 = B(1:18,7) >= 25;
figure(2); subplot(3,1,1);
scatter(x(idx1),y(idx1));
grid on; hold on
lsline
mdl = fitlm(x(idx1),y(idx1));
figure(3); subplot(3,1,1);
scatter(x(idx2),y(idx2));
grid on; hold on
lsline
mdl = fitlm(x(idx2),y(idx2));
end
end
Appreciate the help.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by