MATLAB 交换其枢轴位置为零的行,部分枢轴?
MATLAB swapping rows with zero in its pivot position, partial pivoting?
我想让这个矩阵变成
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
但我不知道在这一点之后我该如何继续。我想编写适用于任何类型矩阵的代码。请帮助。
我想这就是你需要的:
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]
我想让这个矩阵变成
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
但我不知道在这一点之后我该如何继续。我想编写适用于任何类型矩阵的代码。请帮助。
我想这就是你需要的:
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]