For loop is printing the wrong size matrix.

1 view (last 30 days)
I have a matrix of length 9 called average_BMI with the body mass index from 9 different college football teams. The first column of the matrix represents the average BMI of football players from the University of Michigan. I would like to compute the delta BMI by finding the difference between all of the other schools (columns 2-9 of average_BMI) and the average for Michigan. I have written the following for loop to do this.
MI = average_BMI(:,1)
for i = (2:9)
delta_BMI(i) = MI - average_BMI(:,i)
end
However, each time I run this the output matrix has length 9 instead of 8 and starts with 0. Meaning it's not referencing the second column, but instead is finding the difference between Michigan and itself before calculating the rest. Am I inputting the information into the for loop wrong?

Accepted Answer

Matt J
Matt J on 3 Oct 2015
Edited: Matt J on 3 Oct 2015
for i = (2:9)
delta_BMI(:,i-1) = MI - average_BMI(:,i)
end
However, if your original version worked threw no error messages, it means your average_BMI variable is really a vector, and the calculation requires nothing more than,
delta_BMI= average_BMI(1) - average_BMI(2:9)

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!