index exceeds matrix dimensions??

This is part of a loop that I'm trying to run and get an error:
c = zeros(1,20);
for b=1:20
a = 1;
while dH_df(a,b)<0.80 %line 199
a = a+1;
end
c(b) = a;
dH_df(c(b),b);
end
The error is:
"??? Index exceeds matrix dimensions.
Error in ==> filename at 199 while dH_df(a,b)<0.80"
-I tried a few things, but I still can't see where the error stems from A few clarifications: -this loop is part of a bigger program that calculates dH_df
-dH_df is a 31x20 matrix with values ranging from 0.4700 to 1.1100
-the loop is supposed to record the position of values ranging 0.80 to 0.85 from dH_df and record the row position in "c" - I'm having trouble setting up that range as well
Any suggestions will be really appreciated. Thank you.

 채택된 답변

Matt Fig
Matt Fig 2011년 6월 22일

0 개 추천

while a<=size(dH_df,1) && dH_df(a,b)<0.80 %line 199
If you want to find the row position of values between 0.80 and 0.85,
[r,c] = find(dH_df>.8 & dH_df<.85);
r has the row position, c has the column position.

댓글 수: 1

Walter Roberson
Walter Roberson 2011년 6월 22일
Yup. If _all_ the rows in one column satisfied the condition then the original code would try to access past the end of the column.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Sean de Wolski
Sean de Wolski 2011년 6월 22일

0 개 추천

does dH_df of that line have any values <0.8? If it doesn't 'a' will grow to be bigger than the matrix and it would throw that error.
You could replace the while loop with
[a,a] = max(dH_df>=0.80,[],2);
and it will find 'a' for each row all at once (assuming each row has a value less than 0.8, else it'll return the first column.)
Annie M
Annie M 2011년 6월 23일

0 개 추천

Thank you, this:
[r,c] = find(dH_df>.8 & dH_df<.85);
happened to work best with what I'm trying to achieve. The remaining solutions work well, but I would have to state my range differently to avoid the loop trying to access past the end of the column. Thank you very much for everyone's help.

카테고리

도움말 센터File Exchange에서 Matrix Indexing에 대해 자세히 알아보기

질문:

2011년 6월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by