MATLAB在其枢轴位置交换行为零,部分旋转?

时间:2017-04-07 02:26:09

标签: matlab pivot gaussian

我希望这个矩阵变成

A =

     1     2    -1     4     5
     1    -6     7     5     6
     0     0     4     7     5
     0    -3     4     7     7
     1     5     1     5     6

这种矩阵。

A =

     1     2    -1     4     5
     1    -6     7     5     6
     1     5     1     5     6
     0    -3     4     7     7
     0     0     4     7     5

因此,任何在其枢轴位置为零的行都应该下降。

通过使用以下代码,我能够在其枢轴中找到零行:

count=0;
for i=1:n
    for j=1:n
        if A(i,j)~=0
            break
        else
            count=count+1;
            row(count)=i;
            break
        end
    end
end
row

>> row =

    3     4

但我不知道在这一点之后我该怎么做。我想制作适用于任何类型矩阵的代码。 Plz帮助。

1 个答案:

答案 0 :(得分:0)

我认为这就是你所需要的:

A =[1     2    -1     4     5;
    1    -6     7     5     6;
    0     0     4     7     5;
    0    -3     4     7     7;
    1     5     1     5     6];
% find the last zero in each row
[val,lastZeroIdx] = min(A == 0,[],2);
% find rows which their first column is zero
c = A(:,1) == 0;
% eliminate rows which their first column isn't zero
lastZeroIdx(~c) = 0;
% sort by the location of the last zeros
[~,rowsIdx] = sort(lastZeroIdx,'ascend');
% rearrange A
B = A(rowsIdx,:)

你得到:

B = [1     2    -1     4     5
     1    -6     7     5     6
     1     5     1     5     6
     0    -3     4     7     7
     0     0     4     7     5]