[英]Access column of matrix without creating copy of data, using Armadillo library
I have a (data/storage) class with a member arma::mat
(a matrix), and I want to access just one column of this matrix ( arma::colvec
), but I don't want to create a copy of the data in the column. 我有一个(数据/存储)类与成员
arma::mat
(一个矩阵),我想只访问这个矩阵的一列( arma::colvec
),但我不想创建一个副本列中的数据。 Is there any way of achieving this? 有没有办法实现这个目标?
My first try was using .col(i)
, but this returns a temporary, so taking a reference doesn't work. 我的第一次尝试是使用
.col(i)
,但这会返回一个临时的,所以引用它不起作用。
Using .colptr(i)
doesn't create a copy, but it also returns a double*
instead of a vec
, so it makes my life a bit hard. 使用
.colptr(i)
不会创建副本,但它也返回一个double*
而不是vec
,所以它让我的生活变得有点困难。
struct Data
{
mat A;
/* constructors etc. */
// reference to temporary (this is bad)
const vec& getColumn(const uint i) { return A.col(i); }
// doesn't create a copy, but doesn't return a vec either
const double* getColumn2(const uint i) const { return A.colptr(i); }
}
The reason for wanting this is that the class has both vectors and matrices, and I want to make uniform getter and setter methods that access either one of the vectors, or a column in a matrix, depending on an index (for iteration purposes). 想要这个的原因是类有向量和矩阵,我想制作统一的getter和setter方法,它们可以访问其中一个向量,也可以访问矩阵中的一列,具体取决于索引(用于迭代目的)。
An example: 一个例子:
struct Data
{
mat A;
vec b;
// getter
const vec& operator()(const uint i) const
{
if (i == 0)
{
return b;
}
else if (i == 1)
{
// return first column of A
}
// etc.
}
// setter
vec& operator()(const uint i)
{
// similar to above
}
}
Based on the answer by zauguin I found the following way to achieve the wanted functionality: 根据zauguin的回答,我找到了以下方法来实现所需的功能:
struct Data
{
mat A;
vec b;
// getter
subview_col<mat::elem_type> operator()(const uint i)
{
if (i == 0)
{
return b.col(0);
}
else if (i > 0 && i < (1 + A.n_cols()))
{
return A.col(i - 1);
}
else
{
// should probably do some error checking here
}
}
// setter
vec& operator()(const uint i)
{
// same as the above, but without consts in declaration
}
}
This returns subview_col<mat::elem_type>
instead of vec
, but the functionality is similar/identical for my purposes. 这会返回
subview_col<mat::elem_type>
而不是vec
,但功能与我的目的相似/相同。 I am not sure of the performance impact of using .col(0)
on a vec
, but I'm not that worried. 我不确定使用
.col(0)
对vec
的性能影响,但我并不担心。
In arma, A.col(i) doesn't return a reference, but a temporary. 在arma中,A.col(i)不返回引用,而是临时引用。 You can return this temporary to get reference semantics.
您可以返回此临时值以获取引用语义。 With C++14, just write
使用C ++ 14,只需编写
auto getColumn(const uint i) { return A.col(i); }
Otherwise, you have to return the proxy type, it is 否则,您必须返回代理类型,它是
subview_col<mat::elem_type> getColumn(const uint i) { return A.col(i); }
If you have to return a vec
instead of a subview_col<...>
, you can use A.unsafe_col(i)
, but you have to be sure, that you don't try to use it, after the lifetime of A
ended. 如果你必须返回
vec
而不是subview_col<...>
,你可以使用A.unsafe_col(i)
,但你必须确保,在A
的生命周期结束后你不会尝试使用它。 。 see here 看这里
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.