修改下面的c++代码实现将结果存在1列的矩阵中matrixdouble row_major calc_micconst matrixdouble row_major& mat int threads 当样本量小于6个时终止程序 ifmatsize16 stdcerr error the number of data rows should be greater than 5 stdendl
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include
using namespace boost::numeric::ublas;
matrix<double, column_major> calc_mic(const matrix<double, column_major>& mat, int threads) { //当样本量小于6个时,终止程序 if(mat.size1()<6){ std::cerr << "error: the number of data rows should be greater than 5." << std::endl; } if(mat.size2()<2){ std::cerr << "error: the number of data columns should be greater than 1." << std::endl; } // 创建空的对称矩阵 matrix<double, column_major> result(mat.size2(), mat.size2()); // 创建MINE对象
// 计算每列两两之间的mic系数,并填充对称矩阵
omp_set_num_threads(threads);
#pragma omp parallel for
for (size_t i = 0; i < mat.size2(); i++) {
MINE mine(0.5, 15, EST_MIC_APPROX);
for (size_t j = i+1; j < mat.size2(); j++) { // 计算上三角部分
double x[mat.size1()];
double y[mat.size1()];
for (size_t k = 0; k < mat.size1(); k++) {
x[k] = mat(k,i);
y[k] = mat(k,j);
}
mine.compute_score(x, y, mat.size1());
result(i,j) = mine.mic();
result(j,i) = result(i,j); // 填充下三角部分
}
}
return result;
}
原文地址: https://www.cveoy.top/t/topic/iraR 著作权归作者所有。请勿转载和采集!