Problem 44945. Calculate BMI
- 1 kilogram = 2.2 pounds
- 1 inch = 2.54 centimeters
- BMI = weight(kg) / [height(m)]^2
Solution Stats
Problem Comments
-
18 Comments
Nice
function bmi = bmi_calculator(hw)
h_inch = hw(:,1);
h_m = h_inch .* 2.54/100;
w_pounds = hw(:,2);
w_kg = w_pounds .* 0.453592;
BMI = w_kg / (h_m .* h_m);
bmi= BMI(:,end);
end
IM GETTING ASSERTION ERROR....EVEN THOUGH IT GIVES ME EXACT OUTPUT ON MY MACHINE. ANYONE HAVING IDEA ABOUT THIS ?
was getting an assertion error but finally got it after 15 minutes of brainstorming
function bmi = bmi_calculator(hw)
% Convert the height values from inches to meters
height=hw(:,1)./(2.54*10^-2)
% Convert the weight values from lbs to kilograms
weight=hw(:,2)./2.2
% Calculate the bmi for height and weight combination and return the output variable 'bmi'
bmi = weight./(height.^2);
end
can anyone help me to find the aassertion error?
Nice one!
Can anyone explain why
"bmi = convmass(hw(:,2),'lbm','kg') ./ (convlength(hw(:,1),'in','m').^2);"
doesn't work?
@Michael Green: thanks for pointing out convmass and convlength...I did not know that those existed.
Your attempt doesn't work because of conversion round-off. The author indicates that 1" = 2.54cm. That conversion is exact and matches convlength.
The author also indicates that 1kg = 2.2lbs. That conversion is rounded, whereas convmass utilizes a more accurate conversion factor (2.2046). If you use the conversion factor indicated by the author (i.e., don't use convmass), it will work.
My solution passes the first test and I have 4 decimal place accuracy yet I fail the second test. Not sure how this is possible even if the conversion factors aren't the same. By the way, my answer using the values chosen for the second test exactly match those calculated using the NIH calculator.
Good problem
The comments in the solution are there just to help. The solution can be done in a single statement to improve the size.
fun
nice!
function bmi = bmi_calculator(hw)
% Convert the height values from inches to meters
height = hw(:,1).*2.54/100
% Convert the weight values from lbs to kilograms
weight = hw(:,2)./2.2
% Calculate the bmi for height and weight combination and return the output variable 'bmi'
bmi = weight/height.^2;
end
can anyone help me here? I am getting this error
Error using assert The condition input argument must be convertible to a scalar logical. Error in Test2 (line 4) assert(all(abs(bmi_calculator(hw) - bmi_correct) < 1e-4))
@Mohamed in the last line, you're using right matrix division rather than right array division (i.e. element-wise division). Try replacing / with ./ --- that should fix it.
@Christian Thank you very much for the reply. It worked after replacing as you said
function bmi = bmi_calculator(hw)
% Convert the height values from inches to meters
h=reshape(hw(:,1),1,[]);
h=0.0254*h;
% Convert the weight values from lbs to kilograms
w=reshape(hw(:,2),1,[]);
w=(1/2.2)*w;
% Calculate the bmi for height and weight combination and return the output variable 'bmi'
bmi=w./(h.^2);
end
what is wrong with this, please help. It runs fine on RUN button but does not pass the test
@Prathamesh Using your code, bmi is returned as a row vector, but the test suite expects a column vector.
In this excercise the big deal is to recognice that the solution must be in the same format as the entry data. That in this case is a Column Vector.
Solution Comments
Show commentsProblem Recent Solvers9154
Suggested Problems
-
4390 Solvers
-
860 Solvers
-
Matlab Basics - y as a function of x
492 Solvers
-
465 Solvers
-
769 Solvers
More from this Author13
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!