2

I run this code:

for i=1:length(matr)

...where matr is square matrix. In this loop the size of the matr changes, but it seems that the loop continues to run, until i doesn't not exceed the initial value of length(matr)

How to maintain the fresh of length(matr) in the loop's condition?

Here is my code.

for i=1:length(matr1)
       for j=1:length(matr1) 
           if((i~=j)&&(ismember(i,ind3)==0)&&(ismember(j,ind3)==0))
               if (i>length(matr1))||(j>length(matr1))
                   continue
               end
               ind1 = find_tree(matr1,i);
               ind2 = find_tree(matr1,j);
               b = is_isomorphic(matr1(ind1,ind1),matr1(ind2,ind2),encode(ind1),encode(ind2));
               if b,
                   number = number + length(ind1);
                   matr1(ind2,:) = [];
                   matr1(:,ind2) = [];
                   ind3 = find(summ_rows==-1);
               end
           end
       end
    end

I was managed to add

`if (i>length(matr1))||(j>length(matr1))`, 

...because i and j exceeded the dimensions of the matrix.

1
  • 1
    Can you describe what your loop is trying to do? You probably should be using a while loop for this and not a for loop Commented Mar 11, 2014 at 10:28

1 Answer 1

6

You should be using a while loop:

ii = 0;
while(ii <= length(matr))
    ii = ii + 1;

    %// Your loop code here: e.g. the following line that alters the size of matr
    matr = rand(randi(20) + 10);

end
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. And, is it true, that bounds for a for-loop is calculated only before the initial iteration?
@restrest yes that is true as far as I know. Easy for you to test though
I think this might be related to the JIT acceleration. Could be you get different results once you disable it.
@bdecaf I haven't tested it but I wouldn't think so. 1:length(matr1) creates a vector [1,2,3... first and then i will iterate over that. I'm pretty confident that this is set when you create the loop and does not change, JIT or no.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.