首页 > 解决方案 > 如何在犰狳 C++ 中修改矩阵中的某些列

问题描述

我在 Armadillo 10x10 矩阵中有一个非常简单的任务。任务是将列的值1,3,4更改9sqrt(2).

我已经使用 (B is only one) 完成了任务

B.cols(columns) *= sqrt(2);

我为每一列都做了这个并且工作了。

这里的问题是阅读文档可以使用

B.cols( vector of col indices )

我想学习如何使用它,但似乎没有任何效果

标签: c++matrixvectorarmadillo

解决方案


Declare the "vector of col indices" as uvec and put the column indices in it.

mat B(10, 11, fill::randu);

uvec column_indices = { 1, 3, 4, 9 };

B.cols(column_indices) *= sqrt(2);

(Just to be clear, C++ has zero-based indexing, so the first column has index 0, not 1).


推荐阅读