topsis代码的MATLAB实现
时间: 2023-08-06 16:27:19 浏览: 134
以下是一个简单的MATLAB实现Topsis算法的示例代码:
```matlab
% 输入数据矩阵,每行代表一个样本,每列代表一个指标
data = [0.4 0.5 0.6 0.3;
0.3 0.2 0.1 0.7;
0.2 0.3 0.2 0.4;
0.1 0.1 0.1 0.6];
% 权重向量,代表每个指标的重要程度
weights = [0.2 0.3 0.4 0.1];
% 归一化矩阵
norm_data = zeros(size(data));
for i = 1:size(data,2)
norm_data(:,i) = data(:,i)./sqrt(sum(data(:,i).^2));
end
% 极大极小化矩阵
max_data = max(norm_data);
min_data = min(norm_data);
for i = 1:size(norm_data,1)
for j = 1:size(norm_data,2)
norm_data(i,j) = (norm_data(i,j)-min_data(j))/(max_data(j)-min_data(j));
end
end
% 计算正负理想解
ideal_best = max(norm_data);
ideal_worst = min(norm_data);
% 计算距离
dist_best = sqrt(sum((norm_data-ideal_best).^2,2));
dist_worst = sqrt(sum((norm_data-ideal_worst).^2,2));
% 计算综合得分
score = dist_worst./(dist_best+dist_worst);
% 对得分进行排序,得分越高越好
[sorted_score,rank] = sort(score,'descend');
```
在上面的代码中,我们首先输入了一个数据矩阵和一个权重向量,然后进行了归一化和极大极小化处理,计算了每个样本到正负理想解的距离,最后得到了每个样本的综合得分,并按照得分进行了排序。
阅读全文
相关推荐
















