For loop is printing the wrong size matrix.

조회 수: 1 (최근 30일)
Sarah  Brehm
Sarah Brehm 2015년 10월 3일
댓글: Sarah Brehm 2015년 10월 3일
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