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.

 Accepted Answer

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 Comment

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.

Sign in to comment.

More Answers (2)

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.)
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.

Asked:

on 22 Jun 2011

Community Treasure Hunt

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

Start Hunting!