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

回答(1 个)

Steven Lord
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
2 3 4 5
% Looser translation with n = 5
for k = 2:5
disp(k)
end
2 3 4 5
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')
For loop #1 complete
% Loose, n = 1
for k = 2:1
disp(k)
end
disp('For loop #2 complete')
For loop #2 complete

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品


版本

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by