TOPSIS算法程序,包括矩阵列的正向化(将极小型和中间型化为极大型)、矩阵的归一化处理(消除量纲)、最后计算各个元素对应得得分。
代码如下:
clc;clear a = input(''); [m,n] = size(a); while true typ = input('请输入列指标类型 极小型 or 中间型 or 无','s'); if typ == '极小型' index = input('请输入所需正向化列数列表:'); for r = 1:length(index) ma = max(a(:,index(r))); for i = 1:m a(i,index(r)) = ma - a(i,index(r)); end end continue elseif typ == '中间型' index = input('请输入需要正向化列数列表'); center = input('请输入正向化列数中间值列表'); zzz = [index,center]; for i = 1:length(index) for r = 1:length(center) a(r,index(i)) = abs(a(r,index(i))-center(r)); end end continue elseif typ == '无' break else disp('输入无效,请重新输入类型') continue end end disp('正向化后矩阵:') a for i = 1:n a(:,i) = a(:,i)./sqrt(sum(a(:,i).^2)); end disp('归一化处理后矩阵:') a a1 = max(a); a2 = min(a); C = []; for i = 1:m dmax = oushijuli(a1,a(i,:)); dmin = oushijuli(a2,a(i,:)); k = dmin/(dmax + dmin); C = [C,k]; sprintf('第%s个元素的指标:%s',num2str(i),num2str(k)) end [c,index]=sort(C,'descend')