Macaronnos
Macaronnos

Reputation: 647

How to change loop condition in Matlab?

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.

Upvotes: 2

Views: 112

Answers (1)

Dan
Dan

Reputation: 45752

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

Upvotes: 6

Related Questions