TOPSIS的matlab代码
时间: 2024-01-29 22:04:30 浏览: 153
以下是一个简单的TOPSIS MATLAB代码示例:
假设我们有一个决策矩阵D,其中有5个决策和3个评价标准:
```
D = [2 4 3;
5 1 4;
3 2 5;
4 3 2;
1 5 1];
```
我们的权重向量W是[0.4 0.3 0.3],其中每个数字表示每个评价标准的重要程度。现在我们想要用TOPSIS方法来确定哪个决策是最优的。
下面是一个MATLAB函数,它将返回一个向量,其中包含每个决策的TOPSIS得分:
```matlab
function [scores, rank] = topsis(D, W)
% D: decision matrix, n x m
% W: weight vector, 1 x m
% Step 1: normalize the decision matrix
[n, m] = size(D);
D_norm = zeros(n, m);
for i = 1:m
D_norm(:,i) = D(:,i)./sqrt(sum(D(:,i).^2));
end
% Step 2: calculate the weighted normalized decision matrix
D_weighted = zeros(n, m);
for i = 1:m
D_weighted(:,i) = D_norm(:,i).*W(i);
end
% Step 3: determine the ideal and anti-ideal solutions
ideal = max(D_weighted);
anti_ideal = min(D_weighted);
% Step 4: calculate the distance to the ideal and anti-ideal solutions
D_ideal = zeros(n, m);
D_anti_ideal = zeros(n, m);
for i = 1:n
D_ideal(i,:) = sqrt(sum((D_weighted(i,:) - ideal).^2));
D_anti_ideal(i,:) = sqrt(sum((D_weighted(i,:) - anti_ideal).^2));
end
% Step 5: calculate the performance scores
scores = D_anti_ideal./(D_ideal + D_anti_ideal);
% Step 6: rank the decisions based on the performance scores
[~, rank] = sort(scores, 'descend');
end
```
要使用此函数,您可以调用:
```matlab
[scores, rank] = topsis(D, W);
```
其中`scores`是包含每个决策的TOPSIS得分的向量,`rank`是决策的排名。
阅读全文