在 R 中,我可以命名矩阵的行和列:
A = matrix(
c(2, 4, 3, 1, 5, 7), # the data elements
nrow=2, # number of rows
ncol=3, # number of columns
byrow = TRUE) # fill matrix by rows
> A # print the matrix
[,1] [,2] [,3]
[1,] 2 4 3
[2,] 1 5 7
dimnames(A) = list(
c("row1", "row2"), # row names
c("col1", "col2", "col3")) # column names
> A # print A
col1 col2 col3
row1 2 4 3
row2 1 5 7
如何使用 C++ 为 Eigen3 矩阵指定行名和列名?
因为 Eigen 似乎不支持列名和行名,所以我所做的就是自己对其进行哈希处理。更多的打字来让它继续,但完成了工作。
namespace EigenRCNames
{
// The key in my case is a string, but it could be a tuple
//typedef std::tuple<std::string, std::string> rc_key_t;
typedef std::string rc_key_t;
struct key_hash : public std::unary_function<currency_key_t, std::size_t>
{
std::size_t operator()(const rc_key_t& k) const
{
std::hash<std::string> hash_fn;
return hash_fn(k);
}
};
struct key_equal : public std::binary_function<rc_key_t, rc_key_t, bool>
{
bool operator()(c rc_key_t& v0, const rc_key_t& v1) const
{
return (v0 == v1);
}
};
struct data
{
int row;
int column;
inline bool operator ==(data d)
{
if (d.row == row && d.column == column)
return true;
else
return false;
}
friend std::ostream& operator << (std::ostream& os, const data& rhs) //Overloaded operator for '<<'
{ //for struct output
os << rhs.row << ", "
<< rhs.column;
return os;
}
};
typedef std::unordered_map<const rc_key_t, data, key_hash, key_equal> map_t;
// ^ this is our custom hash
}
然后,
//Row STRAWBERRY and Column BANANA maps to {0,0}
static std::string STRAWBERRYBANANA = "STRAWBERRY-BANANA";
data dSTRAWBERRYBANANA = {0, 0};
static map_t m;
m[STRAWBERRYBANANA] = dSTRAWBERRYBANANA;
然后直接通过键“STRAWBERRY-BANANA”或值 {0, 0} 进行搜索。
我是一名优秀的程序员,十分优秀!