EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
这个是只是用行变换将非严格占优矩阵通过行变换转换为严格占有矩阵。 伪代码如下: Input matrix:A Output: If A can transform into a dominance with our method,we can get the dominance. Else output’Cannot 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 A’s 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_row’s 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
0 N% u3 \+ E( Q% SMIE name:ChenYu class:1202 nunmber:12353032- v& D t6 V: L9 S1 {2 p
% Change nondorminant to dorminant matrix
0 {8 V- Q4 T# S1 c5 L% this method is adapted for the matrix which can be changed to dorminant
/ W& ?. `" u0 W" W1 Q) B3 ]- O2 u( I- n% matrix only using row exchangement
6 S9 z" U) F7 G% Input :A matrix: W7 Q( {3 h+ H- H+ d& K5 d
% Output:A matrix which maybe dorminant
3 R4 u2 e0 d" k- V+ Y& Kfunction method1(A); J+ X8 x; ^ j9 |# _) h' D
[row,volumn] = size(A);) }' g/ \" y0 `6 L- F0 b9 ?
record = ones(volumn,3); %use this matrix to record everyline situation
% R( [0 e& _( h, f3 A2 e( ?! c& O' }flag = 1; %first volumn record if the matrix can change to
" n: z) L+ c" U7 r: ^$ @for i = 1:row %dominance,which row the row will �long.Second volumn record
$ E/ e8 H$ x1 g& G8 b every_line = A(i,:); %whether the matrix satisfy the necessity %that everydominance
4 m; M* M0 K3 Q1 d4 f) l; l record(i,3) = i; %third volumn record the rowth
( J5 ^: l! T) l% ?* f [record(i,1),record(i,2)] = which_row(every_line);" M8 J: r2 C6 _+ d9 g: f
if record(i,2) == 0
* @/ D! l0 |* v# y9 {, E disp('This Matrix cannot change to dorminant matrix by row exchange.');. B8 T& [0 I1 B# E( Y* O
flag = 0;; r$ r X* L" {/ k2 U
break;. A6 n# `$ ~- m: w6 w9 [- R# g
end
2 B% F4 _$ K+ D5 c' A1 \9 x/ ^! oend- \% e( B3 t6 C+ A! y
if flag == 1
6 i/ d' _* g: C+ l for i = 1:row-1
0 ~, c, h% e1 c" s9 Z# u; w+ G, N+ h3 a for j = 1:row
& ]3 r5 Z4 e5 Y( J% p8 @/ { if record(j,1) == i %exchange line if flag = 1,which means it can be transformed2 q* ?0 o- E. a" ]' T4 r
b = A(i,:); %into dorminance
2 X& k8 f& |' P9 ?$ p `" ^* u" K A(i,:) = A(record(j,3),:);0 q) I3 r" b+ R
A(record(j,3),:) = b;
- P; f/ N; B1 k# K/ G temp = record(j,:);
+ g! [, P0 A7 [" A6 p0 M2 z- ]5 k8 M record(j,:) = record(i,:);
6 h6 a4 ^$ Z. e/ w. M8 Q( M record(i,:) = temp;3 B5 S& v3 r( Z; {0 H$ j
break;
6 X9 R3 i0 T# T8 G end
5 X; ^( N! k- ^ end
6 H3 [7 R5 j8 z# p; O0 Z; ^! _ end" u/ n- c* R5 j) _! ~
disp(A);
: W$ ]2 ~, B K* H# \end 调用函数: % function judge_row:; U9 I8 V/ ?# J. e1 l: ]
% this function is to judge whether the line is a line of dorminance and
6 X+ w- g' O- {( r3 x n% return which row shuold this line stand5 }; c1 z/ ]" a) @) k
% Input : vector(a row of the matrix)
0 K, e. m8 |# E, J. u% Output:if(the line is dorminant) return flag = 1;
8 ?; [$ d& C6 g* X% [3 ^% else return flag = 0.
6 e* Z8 u3 N; Z* h9 sfunction [row,flag] = which_row(a)
- E" o' t8 N* E8 dn = length(a);, f. {7 W$ J4 _) }3 j
max = a(1);7 }( J) x. u) H
flag = 0;2 b8 e+ Z+ D a2 Y, C$ L+ I7 y
row = 1;$ o$ S3 U' G) {) s% Q9 B
for i = 2:n
5 a6 f* M8 r6 p' M5 _. S if max < a(i) %fing the max value of the line
3 n' Q$ J: G: x$ \- l, y& c' C* R max = a(i); %it's easy for us to know that if the max value is
5 j, s2 F' ~9 a: p" x; `& l8 S row = i; %larger than the rest of all sum
: Z d3 ?4 J+ Y# q6 \7 a end %than the line is dorminance
( h* M6 F* V `: A$ S9 U' ~0 V' E" cend
! l; e2 i3 e3 ?. t# ]) S. Gand = 0;
& V& U4 a- z, b3 W/ k8 z5 Zfor i = 1:n( N+ n m# w! u$ v3 C& [
if i ~= row %compare maxvalue and rest sum
* Q" R# f5 S7 m% X3 w/ a and = and + a(i);
2 [- z. B( D8 L2 \! |4 d g+ J end& L6 l! h; i8 Y- a
end* ^ E: E0 b! K& D& b- F( R" B
if a(row) >= and
/ X6 `1 F1 F) h9 v- r flag = 1;
7 O! k( @0 S) |: A, Zend % C M1 S/ q/ J" }/ i c
|