1、安装eigen3
2、引用头文件
3、代码测试
MatrixXf aaa(2, 4);aaa << 1, 2, 3, 4, 5, 6, 7, 8; Vector2f diff(10, 20);aaa.colwise() += diff;std::cout << "new_aaa : " << aaa << endl;
全部代码:
int main()
{MatrixXf mat(2, 4);mat << -1, 2, 6, 19, //19+6+2-1= 263, 1, 7, -2; // 3+1+7-2=9VectorXf maxVal = mat.rowwise().maxCoeff(); // 计算矩阵钟每行的最大值 返回的是一个行最大值组成的一个向量VectorXf minVal = mat.rowwise().minCoeff(); // 同上std::cout << "Maxima at positions " << endl;// std::cout << maxIndex << std::endl;std::cout << "maxVal : " << maxVal << endl;std::cout << "minVal : " << minVal << endl;VectorXf difference = maxVal - minVal; // 对应的行 maxVal[i]-minVal[i],返回的也是一个向量std::cout << "difference : " << difference << endl;Vector2f mm(-1,-2);std::cout << "difference.norm() : " << difference.norm() << endl; // 计算模 std::cout << "mm.norm() : " << mm.norm() << endl;// 计算每行的和 返回的是一个行向量 Vector2f source_mea=mat.rowwise().sum();std::cout << "source_mea.norm() : " << source_mea<< endl;cout << "矩阵的列数 " << mat.cols() << endl; // 4 // 计算没行的平均值Vector2f mean=source_mea / 4;std::cout << "mean : " << mean << endl;MatrixXf vertices_source(2, 4);vertices_source << -1, 2, 6, 19, //19+6+2-1= 263, 1, 7, -2; // 3+1+7-2=9MatrixXf vertices_target(2, 4);vertices_target << 0, 2, 6, 19, //19+6+2-1= 263, 1, 7, 2; // 3+1+7-2=9//最大 最小 Vector2f source_scale = vertices_source.rowwise().maxCoeff() - vertices_source.rowwise().minCoeff();Vector2f target_scale = vertices_target.rowwise().maxCoeff() - vertices_target.rowwise().minCoeff();double scale = std::max(source_scale.norm(), target_scale.norm());std::cout << "scale = " << scale << std::endl;vertices_source /= scale;vertices_target /= scale;std::cout << "vertices_source : " << vertices_source << endl;std::cout << "vertices_target : " << vertices_target << endl;/// De-meanVector2f source_mean, target_mean;source_mean = vertices_source.rowwise().sum() / double(vertices_source.cols());target_mean = vertices_target.rowwise().sum() / double(vertices_target.cols());std::cout << "source_mean : " << source_mean << endl;std::cout << "target_mean : " << target_mean << endl;vertices_source.colwise() += source_mean;vertices_target.colwise() += target_mean;std::cout << "vertices_source : " << vertices_source << endl;std::cout << "vertices_target : " << vertices_target << endl;std::cout << "aaa =========================================== : " << endl;MatrixXf aaa(2, 4);aaa << 1, 2, 3, 4, //5, 6, 7, 8; // Vector2f diff(10, 20);aaa.colwise() += diff;std::cout << "new_aaa : " << aaa << endl;system("pause");return 0;}
先更新到此,后续碰到再说。。。。。。