matlab 阶梯形矩阵,求化矩阵为阶梯型矩阵的代码(不是行最简函数rref)

下面是matlab中rref函数的代码,求将其改为化矩阵为行阶梯型矩阵的函数,

把一个矩阵化为行最简型应该是先从上至下化为行阶梯,再从下往上化为行最简!

function [A,jb] = rref(A,tol)

%RREF   Reduced row echelon form.

%   R = RREF(A) produces the reduced row echelon form of A.

%

%   [R,jb] = RREF(A) also returns a vector, jb, so that:

%       r = length(jb) is this algorithm's idea of the rank of A,

%       x(jb) are the bound variables in a linear system, Ax = b,

%       A(:,jb) is a basis for the range of A,

%       R(1:r,jb) is the r-by-r identity matrix.

%

%   [R,jb] = RREF(A,TOL) uses the given tolerance in the rank tests.

%

%   Roundoff errors may cause this algorithm to compute a different

%   value for the rank than RANK, ORTH and NULL.

%

%   Class support for input A:

%      float: double, single

%

%   See also RANK, ORTH, NULL, QR, SVD.

%   Copyright 1984-2005 The MathWorks, Inc.

[m,n] = size(A);

% Does it appear that elements of A are ratios of small integers?

[num, den] = rat(A);

rats = isequal(A,num./den);

% Compute the default tolerance if none was provided.

if (nargin < 2), tol = max(m,n)*eps(class(A))*norm(A,'inf'); end

% Loop over the entire matrix.

i = 1;

j = 1;

jb = [];

while (i <= m) && (j <= n)

% Find value and index of largest element in the remainder of column j.

[p,k] = max(abs(A(i:m,j))); k = k+i-1;

if (p <= tol)

% The column is negligible, zero it out.

A(i:m,j) = zeros(m-i+1,1);

j = j + 1;

else

% Remember column index

jb = [jb j];

% Swap i-th and k-th rows.

A([i k],j:n) = A([k i],j:n);

% Divide the pivot row by the pivot element.

A(i,j:n) = A(i,j:n)/A(i,j);

% Subtract multiples of the pivot row from all the other rows.

for k = [1:i-1 i+1:m]

A(k,j:n) = A(k,j:n) - A(k,j)*A(i,j:n);

end

i = i + 1;

j = j + 1;

end

end

% Return "rational" numbers if appropriate.

if rats

[num,den] = rat(A);

A=num./den;

end

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值