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.
Star Strider
Star Strider 2017 年 3 月 19 日
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.
John D'Errico
John D'Errico 2017 年 3 月 19 日
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 日

0 投票

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 ExchangeStartup 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