层次分析(AHP),以一个准则层判断矩阵和四个方案层判断矩阵为例,包括一致性检验、特征向量、特征值的计算。我们以特征向量作权重向量,直接输出。
代码如下:
format short
clc; clear
ri=[0,0,0.58,0.90,1.12,1.24,1.32,1.41,1.45];
A=input('请输入决策层判断矩阵:');
B = input('请输入方案层判断矩阵集合:');
[C,D]=eig(A);
shape = length(eig(D));
lamda = max(diag(D));
RI = ri(length(D))
CI = (roundn(lamda,-10)-shape)/(shape-1)
CR = CI/RI
if CR<0.10
loc = find(diag(D)==lamda);
W0 = C(:,loc)./sum(C(:,loc))
else
disp('该矩阵未通过一致性检验,请修正后重试')
end
C = [];
for i = 1:length(B)
sprintf('方案层第%s个判断矩阵',num2str(i))
[E,F] = eig(B{i});
shape = length(eig(B{1}));
lamda = max(diag(F));
RI = ri(length(B{1}))
CI = (roundn(lamda,-10)-shape)/(shape-1)
CR = CI/RI
if CR<0.10
loc = find(diag(F)==lamda);
Wi = E(:,loc)./sum(E(:,loc))
C = [C,Wi];
else
disp('该矩阵未通过一致性检验,请修正后重试')
end
end
goal = C*W0