How to continue performing this flowchart about Gauss - Jordan method in a matlab code?
2 次查看(过去 30 天)
显示 更早的评论
% I'm using matlab to convert this flowchart in a matlab code using "for loop", but I don't know how to continue here in this point. I guess it is possible to use else - if, but I´'m not sure. Please, could you check that?
%Here is the code that I did, but I don't know how to continue:
%Equations SOLVER by Gauss-Jordan METHOD
G=input('Put the n*(n+1) matrix to solve: ');
sz=size(G)
n=sz(1)
for i=1:n
c=G(i,i)
for j=1:n+1
G(i,j)=G(i,j)/c;
end
for k=1:n
0 个评论
回答(1 个)
Steven Lord
2022-7-26
That check basically says to skip the first iteration of the loop. You could do this with an if statement and a continue statement, but rather than translate this strictly I'd probably instead just start the loop with k = 2.
% Strict translation with n = 5
for k = 1:5
if k == 1
continue
end
disp(k)
end
% Looser translation with n = 5
for k = 2:5
disp(k)
end
If n is less than 2 the loop over k won't do anything anyway, so starting at k = 2 doesn't cause any problems. Neither of the code examples below will display anything (other than the message that the loop is complete, since I wanted to prove to you that the code did in fact run.)
% Strict, n = 1
for k = 1:1
if k == 1
continue
end
disp(k)
end
disp('For loop #1 complete')
% Loose, n = 1
for k = 2:1
disp(k)
end
disp('For loop #2 complete')
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!