C++ Armadillo 重塑一个只有一维大小的矩阵

C++ Armadillo reshape a matrix with only one dimension size

使用 Armadillo,当我只指定一维大小时如何重塑矩阵?

Matlab documentation 中,有这样一个功能示例:

Reshape a 6-by-6 magic square matrix into a matrix that has only 3 columns. Specify [] for the first dimension size to let reshape automatically calculate the appropriate number of rows.

A = magic(6);
B = reshape(A,[],3);

The result is a 12-by-3 matrix, which maintains the same number of elements (36) as the original 6-by-6 matrix. The elements in B also maintain their columnwise order from A.

Armadillo 如何做到这一点?

您可以使用 .size() 来获取矩阵的元素总数并自行计算维度。 示例:

B = reshape(A, A.size()/3, 3);