How to plot in for loop?

1 回表示 (過去 30 日間)
torre
torre 2019 年 9 月 21 日
コメント済み: torre 2019 年 9 月 24 日
I'm trying to plot in loop a vector that has body mass index values. Purpose is to plot different weight category like undereight in blue and overweight in red and so on but i can't get my loop working properly. Can somebody help?
here is my code
data =[75 67 43 56 78 49 66 71 120
164 168 152 169 170 157 167 181 170];
weight=data(1,:)
heigth=data(2,:)
bmi=weight./((heigth/100).^2);
for i = 1:length(bmi)
if i<18.5
scatter(heigth,weight,i,'b*')
elseif i>24.9
scatter(heigth,weight,i,'r*')
else
scatter(heigth,weight,i,'g*')
end
end
grid on
xlabel('height (m)')
ylabel('weight (kg)')

採用された回答

darova
darova 2019 年 9 月 21 日
You re comparing wrong variable
if i<18.5 % maybe bmi(i) < 18.5
Forgot hold on
hold on
You draw all data together instead of one point
scatter(heigth,weight,i,'r*') % maybe plot(heigth(i),weight(i),'r*')
  1 件のコメント
torre
torre 2019 年 9 月 24 日
Thank you. Now I understand the logic in this code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by