How to plot in for loop?

2 Ansichten (letzte 30 Tage)
torre
torre am 21 Sep. 2019
Kommentiert: torre am 24 Sep. 2019
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)')

Akzeptierte Antwort

darova
darova am 21 Sep. 2019
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 Kommentar
torre
torre am 24 Sep. 2019
Thank you. Now I understand the logic in this code.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by