Problem 44945. Calculate BMI
Solution Stats
Problem Comments

12 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 roundoff. 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!
Solution Comments

1 Comment
Quite challenging! Nice but you've got loads of things to improve je, je :d

1 Comment
We had a problem here. Check out your excersice asap pls.

1 Comment
%colnames = {'height' 'weight'};
%stable = array2table(hw,'VariableNames',colnames)
%stable = [0.01*2.5*stable.height,stable.weight/(2.2)]
%result= stable(:,2)/stable(:,1).^2
%bmi = result(:,5)
Can somebody explain why the assertain is failing in this approach?

2 Comments
The tolerance in assert is very high. Its not worthwhile to tune the decimal places to get that tolerance.
Reconsider the problem and reduce the tolerance.
This is the standard tolerance used in Cody. You'll encounter that frequently with many problems involving floating point numbers. There is nothing unsolvable/hard specifically about it.

1 Comment
interesting question

1 Comment
nice problem

1 Comment
Your first verification is FALSE

1 Comment
be careful about the unit convertion,
height = hw(:,1) * 2.54 / 100;

2 Comments
I made a good answer. and I was able to test hem in my desktop MATLAB but I got error with testing the first status here "error using assert"
Your function is returning the result as a row vector rather than a column vector. MATLAB defaults to writing results as a row vector when only one dimension is specifed, so change your forloop assignments to bmi(i,1) = ... to force a column vector (or transpose bmi after the loop).

3 Comments
Please, do we need to sum up values of all rolls and columns respectively to calculate the BMI?
No. Use Elementwise operations.
Using regex just to wrap the entire code is just a tricky way of winning. The solution is no better than the others.

4 Comments
I solved the problem but the weird thing is that I had to multiply the bmi result with 10000 ro get the right results. Somehow the results without the multiplication were not the exact results. Have anyone faced the same things?
Did you use height in cm instead of in metres?
You have converted inches to centimeters and not meters by using the conversion factor 2.54.
Ah yes, thank you for pointing out guys, and sorry for the late response :/

1 Comment
"The condition input argument must be convertible to a scalar logical."
how this error is rectified?

1 Comment
Dividing height in inches by dividing by 39.37 fails. However, 39.3701 passes first test but fails the second. I had to find a closer value to satisfy second test, 39.37008. You should improve the test parameters.

1 Comment
bmi_correct and bmi_calculator are same. even then I am getting assertion error

1 Comment
What is wrong i this code, The final answers are also matching but it is showing as a fail, I am not getting it.

1 Comment
Leading solution is not correct. The code has comment to convert the height and weight seperately followed by the bmi computation. bmi should not be calculated in one line of code as in leading solution by crunching all the factors.

1 Comment
a very narrow range of assertions. The solution is correct, but can not proceed due to narrow limits of the assertions.

1 Comment
The problem specifies that the input hw is a matrix with two columns (not a table). So, the error is saying that you cannot use dot indexing on a matrix.

1 Comment
Didn't know this is how simple BMI is.

2 Comments
I've got the right values but I've failed the assertion somehow?
The expected output is a column vector containing ALL the bmi values, whereas your code returns a single bmi value i.e. the last one. Hope that helps!

1 Comment
increase the tolerance value..i had to be very specific to match the result

1 Comment
Nice!
Problem Recent Solvers6817
Suggested Problems

3651 Solvers

4252 Solvers

2915 Solvers

437 Solvers

264 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!