For loop is printing the wrong size matrix.

1 次查看(过去 30 天)
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?

采纳的回答

Matt J
Matt J 2015-10-3
编辑:Matt J 2015-10-3
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)

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by