How to continue performing this flowchart about Gauss - Jordan method in a matlab code?
조회 수: 1 (최근 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?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1077255/image.png)
%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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!