For loop is printing the wrong size matrix.

1 vue (au cours des 30 derniers jours)
Sarah  Brehm
Sarah Brehm le 3 Oct 2015
Commenté : Sarah Brehm le 3 Oct 2015
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?

Réponse acceptée

Matt J
Matt J le 3 Oct 2015
Modifié(e) : Matt J le 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)

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by