#include "AverageMethod.h" #include #include AverageMethod::AverageMethod(const QVector &rMat, const QVector &wMat, const QVector &maxMat, const QVector &minMat, const QVector &sumMat, int type) : m_rMat(rMat), m_wMat(wMat), m_maxMat(maxMat), m_minMat(minMat), m_sumMat(sumMat) { Q_ASSERT(rMat.count() != 0); Q_ASSERT(rMat.at(0).count() != 0); if (type == MethodType::Max) { for (auto &r : m_rMat) { for (int i = 0; i < r.count(); ++i) { r[i] = r[i] / m_maxMat.at(i); } } } else if (type == MethodType::Min) { for (auto &r : m_rMat) { for (int i = 0; i < r.count(); ++i) { r[i] = r[i] / m_minMat.at(i); } } } else { // Z-Score 需要求方差和均值 QVector aMat; QVector sMat; int count = m_rMat.count(); for (auto s : m_sumMat) { aMat.append(s / count); } //方差 int rCount = m_sumMat.count(); for (int i = 0; i < rCount; ++i) { qreal s2 = 0; for (auto r : m_rMat) { s2 += pow(r.at(i) - aMat.at(i), 2); } sMat.append(sqrt(s2 / count)); } for (auto &r : m_rMat) { for (int i = 0; i < r.count(); ++i) { r[i] = (r[i] - aMat.at(i)) / sMat.at(i); } } } } AverageMethod::Relations AverageMethod::getRelations() const { Relations uMat; for (auto r : m_rMat) { qreal ui = 0; for (int i = 0; i < r.count(); ++i) { ui += r.at(i) * m_wMat.at(i); } uMat << ui; } return uMat; }