Matlab迭代删除一行矩阵,除了第一次迭代
Matlab Iteratively remove a row of matrix, except for first iteration
我有一个矩阵,用于使用 ode45 求解方程组。我在 while 循环中迭代地删除一行。但是,我希望第一次迭代 'continue' 并且不删除任何箭头,这样我就可以将删除一个数组的所有结果与使用初始矩阵的结果进行比较。
看起来像:
while smtg
A(pos,:)=0 % Do not compute this line the first iteration
pop=ode45(involving A)
end
我的真实代码:
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
我尝试使用
if countrow==1 % (the first iteration),
continue;
end
但它会跳到第二个结尾并且不计算 nbtot 所以我没有想法......有什么帮助吗?
如果您不希望 A(pos,:)=0; % remove one row
出现在第一次迭代中,则将其放在 if
语句中,例如
countrow = countrow + 1;
if countrow ~= 1
A(pos,:)=0; % remove one row
end
MATLAB中的~=
符号是"not equal to"。 A(pos,:)=0; % remove one row
行现在仅在 countrow
为 而不是 等于 1
时执行,但循环中的所有其他语句将正常执行。
循环中的关键字continue
will stop the execution of the current loop iteration and proceed to the next loop iteration. All of the statements after a continue
不会被执行。
或者您可以选择:
A(pos,:)=A(pos,:)*(countrow==1);
我有一个矩阵,用于使用 ode45 求解方程组。我在 while 循环中迭代地删除一行。但是,我希望第一次迭代 'continue' 并且不删除任何箭头,这样我就可以将删除一个数组的所有结果与使用初始矩阵的结果进行比较。
看起来像:
while smtg
A(pos,:)=0 % Do not compute this line the first iteration
pop=ode45(involving A)
end
我的真实代码:
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
我尝试使用
if countrow==1 % (the first iteration),
continue;
end
但它会跳到第二个结尾并且不计算 nbtot 所以我没有想法......有什么帮助吗?
如果您不希望 A(pos,:)=0; % remove one row
出现在第一次迭代中,则将其放在 if
语句中,例如
countrow = countrow + 1;
if countrow ~= 1
A(pos,:)=0; % remove one row
end
MATLAB中的~=
符号是"not equal to"。 A(pos,:)=0; % remove one row
行现在仅在 countrow
为 而不是 等于 1
时执行,但循环中的所有其他语句将正常执行。
循环中的关键字continue
will stop the execution of the current loop iteration and proceed to the next loop iteration. All of the statements after a continue
不会被执行。
或者您可以选择:
A(pos,:)=A(pos,:)*(countrow==1);