user3767071
user3767071

Reputation: 109

Matlab Iteratively remove a row of matrix, except for first iteration

I have a matrix that I use to solve a system of equation using ode45. I am iteratively removing one row in a while loop. However, I want the first iteration to 'continue' and not remove any arrow, so that I can compare all my results where one array is removed with the result using initial matrix.

It would look like:

while smtg
    A(pos,:)=0 % Do not compute this line the first iteration
    pop=ode45(involving A)
end

My real code:

countrow=0;
A=randi([0 1], 5, 5);
distrib=sum(A,1);      
while sum(distrib)>5 
    countrow=countrow+1; 
    A(pos,:)=0; % remove one row
    options = odeset('RelTol', 1e-4);
    [t, pop]=ode45(@Diff,[0 MaxTime],[E I],options,n,m, A);
    nbtot =sum(pop(:,n+1:2*n),2);
end

I tried to use

if countrow==1 % (the first iteration),
    continue;
end

but it skips until the second end and not compute nbtot so I'm out of ideas... Any help?

Upvotes: 0

Views: 72

Answers (2)

IKavanagh
IKavanagh

Reputation: 6187

If you don't want A(pos,:)=0; % remove one row to occur on the first iteration then put it inside an if statement like

countrow = countrow + 1;
if countrow ~= 1
    A(pos,:)=0; % remove one row
end

The ~= symbol in MATLAB is "not equal to". The line A(pos,:)=0; % remove one row will now only execute when countrow is not equal to 1 but all of the other statements within the loop will execute as normal.


The keyword continue will stop the execution of the current loop iteration and proceed to the next loop iteration. All of the statements after a continue within a loop will not be executed.

Upvotes: 1

BillBokeey
BillBokeey

Reputation: 3476

Or you can go with :

A(pos,:)=A(pos,:)*(countrow==1);

Upvotes: 1

Related Questions