简体   繁体   English

Armadillo C ++:如何使用来自另一个矩阵的多个元素修改矩阵的多个数组元素,特别是在立方体结构中?

[英]Armadillo C++: How to modify multiple array elements of a matrix using multiple elements from another matrix, specifically in a cube structure?

In MATLAB, if I have two 3x3 matrices A and B and want to copy the last two columns of B into the first two of A, I would use the following syntax: 在MATLAB中,如果我有两个3x3矩阵A和B并且想要将B的最后两列复制到A的前两个中,我将使用以下语法:

A(:,1:2) = B(:,2:3)

I am trying to complete the same action using Armadillo in C++, specifically for a cube structure. 我试图在C ++中使用Armadillo完成相同的操作,特别是对于立方体结构。 In Armadillo, if I had two cubes A and B with nine slices (with each slice being a 3x3 matrix), I assumed I would use the following to perform the same column element update: 在犰狳中,如果我有两个立方体A和B有九个切片(每个切片是一个3x3矩阵),我假设我将使用以下内容执行相同的列元素更新:

A(span(0,2),span(0,1),span(i)) = B(span(0,2),span(1,2),span(i))

where 'i' is just the slice index. 其中'i'只是切片索引。 The syntax is based on the Armadillo syntax guide. 语法基于Armadillo语法指南。

The code compiles without error and runs; 代码编译没有错误并运行; the cube slices are just not being updated. 多维数据集切片只是没有更新。 Am I using the correct Armadillo syntax here, and is this the most efficient way to perform these operations? 我在这里使用正确的Armadillo语法,这是执行这些操作的最有效方法吗?

If you want a simple copy/paste (not a shift/rotation of your cube matrices), you can use this syntax that do the trick : 如果你想要一个简单的复制/粘贴(不是你的立方体矩阵的移位/旋转),你可以使用这种方法来解决这个问题:

#include <armadillo>

int main (int argc, char* argv[])
{
    size_t num_slices = 9;

    arma::icube A (3, 3, num_slices);
    arma::icube B (3, 3, num_slices);

    A.zeros();
    B.randn();

    A.print("Cube A :\n");
    B.print("Cube B :\n");

    for (int s = 0; s < num_slices; ++s)
        A.slice(s)(arma::span::all, arma::span(0, 1)) = 
        B.slice(s)(arma::span::all, arma::span(1, 2));

    A.print("Cube A :\n");
    B.print("Cube B :\n");

    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM