本文介绍了犰狳C ++:从数组初始化矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来使用犰狳,而无法得到尽管试图/搜索颇有几分以下。

I am new to using armadillo, and could not get the following in spite of trying / searching quite a bit.

有两个巨大的(动态)阵列(而不是矢量),我需要在执行的相关性。我决心用犰狳这一点。我明白了如何使用向量初始化ARMA ::垫,但我可以用数组这样做呢?我明白不是我没有看到在任何提及。
我想,以避免内部设计原因,使用向量。
我试着手动使用初始化数组样本的每个元素(作为一个愚蠢的,但起点)。像下面code的东西是行不通的。

There are two huge (dynamic) arrays (not vectors) that I need to perform correlation on. I resolved to use armadillo for this. I understand how to initialize arma::mat using vectors, but can I use arrays to do so? I understand not as I don't see any mention in the documentation.I am trying to avoid the use of vectors for internal design reasons.I tried manually initializing each element using sample arrays (as a dumb but starting point). Something like the following code wouldn't work.

using namespace std;
using namespace arma;

mat A(SIZE, 1), B(SIZE, 1);

for(int i = 0; i < SIZE; i++)
{
    A << v[i] << endr;
    B << c[i] << endr;
}

cout << "A: " << endl;
A.print();
cout << "B: " << endl;
B.print();

有关的输入数组V = {1,2,0,-1,0.9}和c = {0,5,1,-2,-5}。输出将是:

For the input arrays v = {1, 2, 0, -1, .9} and c = {0, .5, 1, -2, -5}. The output will be:

A:
        0
B:
  -5.0000

这是可以理解的。
任何工作都围绕初始化ARMA ::垫或ARMA :: colvector使用数组?在此先感谢!

which is understandable.Any work around for initializing arma::mat or arma::colvector with arrays? Thanks in advance!

推荐答案

假设你的数组V和C是双数组,你可以使用AUX内存构造函数:

Assuming that your arrays v and c are double arrays, you can just use the aux memory constructors:

犰狳DOC :


      
  • 垫(aux_mem *,n_rows,n_cols,copy_aux_mem = TRUE,严格= TRUE)

使用来自可写的辅助存储器中的数据矩阵。默认矩阵从辅助存储器(安全)分配它自己的存储器和复制数据。但是,如果copy_aux_mem设置为false,矩阵将改为直接使用辅助存储器(即不复制)。这是更快,但可能是危险的,除非你知道你在做什么!

Create a matrix using data from writeable auxiliary memory. By default the matrix allocates its own memory and copies data from the auxiliary memory (for safety). However, if copy_aux_mem is set to false, the matrix will instead directly use the auxiliary memory (ie. no copying). This is faster, but can be dangerous unless you know what you're doing!

严格的变量生效只有copy_aux_mem的(即矩阵直接使用辅助存储器)设置为false。如果严格设置为true,矩阵将被绑定到其生命周期的辅助存储器;在矩阵元素的数目不能被改变(直接或间接)。如果严格设置为假,基质将不绑定到其寿命,即在辅助存储器中,矩阵的大小是可以改变的。如果元素的请求的数目是与辅助存储器的大小不同,新的存储器将被分配和辅助存储器将不再使用。

The strict variable comes into effect only if copy_aux_mem is set to false (ie. the matrix is directly using auxiliary memory). If strict is set to true, the matrix will be bound to the auxiliary memory for its lifetime; the number of elements in the matrix can't be changed (directly or indirectly). If strict is set to false, the matrix will not be bound to the auxiliary memory for its lifetime, ie., the size of the matrix can be changed. If the requested number of elements is different to the size of the auxiliary memory, new memory will be allocated and the auxiliary memory will no longer be used.


      
  • 垫(常量aux_mem *,n_rows,n_cols)

  •   

从只读辅助存储器复制数据创建一个矩阵。

Create a matrix by copying data from read-only auxiliary memory.

这意味着你可以通过复制源数据像这样创建矩阵:

Which means you can create your matrixes by copying your source data like this:

mat A_Copy(v, SIZE, 1);
mat B_Copy(c, SIZE, 1);

或者,你其实可以重用你已经分配的内存为您的阵列创建只读矩阵,像这样的:

Or you can actually reuse the memory you already have allocated for your arrays to create read-only matrixes, like this:

mat A_InPlace(v, SIZE, 1, /*copy_aux_mem*/false, /*strict*/true);
mat B_InPlace(c, SIZE, 1, /*copy_aux_mem*/false, /*strict*/true);

如果你使用这种媒介变得更加简单。

This gets even simpler if you use vectors

vec A_Vec_Copy(v, SIZE);
vec B_Vec_Copy(c, SIZE);

或者

vec A_Vec_InPlace(v, SIZE, false, true);
vec B_Vec_InPlace(c, SIZE, false, true);

这篇关于犰狳C ++:从数组初始化矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:14