For loop is printing the wrong size matrix.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
Sarah Brehm
el 3 de Oct. de 2015
Comentada: Sarah Brehm
el 3 de Oct. de 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?
0 comentarios
Respuesta aceptada
Matt J
el 3 de Oct. de 2015
Editada: Matt J
el 3 de Oct. de 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)
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!