在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 Matrix提供行名和列名?
答案 0 :(得分:1)
我做了什么,因为看起来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}进行搜索是非常困难的。