Matlab Code for a rectangular matrix in row echelon form?

A=[1,2,1,3,3;2,4,0,4,4;1,2,3,5,5;2,4,0,4,7]
[n1,n2]=size(A); % # of rows and columns
p = (1:n1)'; % # each row in the matrix to pivot if necessary
for k = 1:n1-1 % Repeat the loop based on 1 - #rows
[r,m] = max(abs(A(k:n1,k))) % r is the value of the max in the kth col and m is the
% matrix location
m = m+k-1 %Previous max matrix location plus the next 'k' step in the matrix
if r~=0 %Checks to see if the col is zero
break;
end
end
if (m~=k) %Checks to see if the max vector location equals the the next location
%step in the matrix. If not the switch the rows.
A([k m],:) = A([m,k],:)
p([k m]) = p([m k])
end
i = k+1:n1; %Count for the next row
C(i,k) = A(i,k)/A(k,k) %Storing the multipliers for the kth col in Matrix C
j = k:n2 %Counting columns
A(i,j) = A(i,j)-C(i,k)*A(k,j) %Using the multiplier to complete forward elimination
Please see the matlab code that I wrote above, it did not complete the rectangular matrix in row echelon form. It only completed one cycle, I am having difficulty telling it to skip the zero column and check for the max in the adjacent column. It seems to stop when it gets to the zero column and not move on to check the next column. Can you please help?
Your help is greatly appreciated.
Thank you,
Addanis

댓글 수: 4

John D'Errico
John D'Errico 2017년 3월 19일
편집: John D'Errico 2017년 3월 19일
Please learn to format your code so it is readable. Read this .
Help those whom you would ask to help you. Make their job easier, and you will get help more quickly.
The rref function will do this for you.
John D'Errico
John D'Errico 2017년 3월 19일
편집: John D'Errico 2017년 3월 19일
I somehow doubt that rref would be an acceptable solution to a homework problem. :)
At the same time, Paul DID post a not terrible attempt at code. It even seems to have comments in it. If I could read it, I'd be willing to take a look for the problem, after breakfast.
I've edited your code, making it readable. Please see that it can now be read as code. Done once for you.

댓글을 달려면 로그인하십시오.

답변 (1개)

Rahul Mishra
Rahul Mishra 2022년 4월 28일
m=input('no. of rows');
n=input('no. of columns');
for i=1:m
for j=1:n
A(i,j)=input('enter the entry');
end
end
for z=1:m-1
if(A(z,z)~=0)
a=A(z,z)
for k=z+1:m
b=A(k,z)
for l=1:n
A(k,l)=a*A(k,l)-b*A(z,l)
end
end
end
end
disp(A)

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

질문:

2017년 3월 18일

답변:

2022년 4월 28일

Community Treasure Hunt

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

Start Hunting!

Translated by