Matlab之将非严格占优矩阵化为严格占优矩阵

    这个是只是用行变换将非严格占优矩阵通过行变换转换为严格占有矩阵。

伪代码如下:

Input matrix:A

Output:

      If A can transform into a dominance with our method,we can get the dominance.

      Else outputCannot transform the matrix into dominance

Variable:

Set a matrix record with the size [row_of_matrix,3]

Record[row_length_matrix,3]  %This matrix is for record As situation. 

% Record[1:,1] first volumn stands for this row of A should be put which row

% Record[1:,2] second volumn stands for whether this row satisfy the necessity of transform into a dominance.If yes,set 1;otherwise 0.

% Record[1:,3] third volumn stands for which initial row belong to.

Flag% this variable is to record whether the matrix A can transform into a dominance.

% If yes,Flag = 1;

% otherwise Flag = 0

% beneath code is to test whether the matrix can transform into a dominance

% and record the situation of every row. 

for i = 1:size_of_matrix

Test every row(Record);

If test_rows Record[1:,2] == 0 

    then break and output Cannot transform the matrix into dominance

    Flag = 0;

end

end

% If Flag = 1,we use row exchangement to transform A into dominance

If Flag = 1

   Row_exchangment by record.

   for i = 1:row-1

       for j = 1:row

            if record(j,1) == i         %exchange line if flag = 1,which    

                %means it can be transformed into dominance

                exchange A(i,:) and A(record(j,3),:);

                exchange record(j,:) and record(i,:);              

                break;

            end

        end

    end

    Display A;

end

具体实现代码如下:

% Project 1
% SMIE   name:ChenYu   class:1202   nunmber:12353032
% Change nondorminant to dorminant matrix
% this method is adapted for the matrix which can be changed to dorminant
% matrix only using row exchangement
% Input :A matrix
% Output:A matrix which maybe dorminant
function method1(A)
[row,volumn] = size(A);
record = ones(volumn,3);                 %use this matrix to record everyline situation
flag   = 1;                              %first volumn record if the matrix can change to
for i = 1:row                            %dominance,which row the row will

                                         �long.Second volumn record
    every_line                = A(i,:);  %whether the matrix satisfy the necessity

                                         %that everydominance
    record(i,3)               = i;       %third volumn record the rowth
    [record(i,1),record(i,2)] = which_row(every_line);
    if  record(i,2)           == 0
        disp('This Matrix cannot change to dorminant matrix by row exchange.');
        flag                  = 0;
        break;
    end
end
if flag == 1
    for i = 1:row-1
        for j = 1:row
            if record(j,1) == i         %exchange line if flag = 1,which means it can be transformed
                b                = A(i,:);             %into dorminance
                A(i,:)           = A(record(j,3),:);
                A(record(j,3),:) = b;
                temp             = record(j,:);
                record(j,:)      = record(i,:);
                record(i,:)      = temp;
                break;
            end
        end
    end
    disp(A);
end

调用函数:

% function judge_row:
% this function is to judge whether the line is a line of dorminance and
% return which row shuold this line stand
% Input : vector(a row of the matrix)
% Output:if(the line is dorminant) return flag = 1;
%                             else return flag = 0.
function [row,flag] = which_row(a)
n = length(a);
max  = a(1);
flag = 0;
row  = 1;
for i = 2:n
    if max < a(i)          %fing the max value of the line
       max = a(i);         %it's easy for us to know that if the max value is
       row = i;            %larger than the rest of all sum
    end                    %than the line is dorminance
end
and = 0;
for i = 1:n
   if i ~= row             %compare maxvalue and rest sum
        and = and + a(i);
    end
end
if a(row) >= and
    flag = 1;
end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值