Matlab Code for a rectangular matrix in row echelon form?
Show older comments
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 Comments
John D'Errico
on 19 Mar 2017
Edited: John D'Errico
on 19 Mar 2017
Help those whom you would ask to help you. Make their job easier, and you will get help more quickly.
Star Strider
on 19 Mar 2017
The rref function will do this for you.
John D'Errico
on 19 Mar 2017
Edited: John D'Errico
on 19 Mar 2017
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.
John D'Errico
on 19 Mar 2017
I've edited your code, making it readable. Please see that it can now be read as code. Done once for you.
Answers (1)
Rahul Mishra
on 28 Apr 2022
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)
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!