特征 3:矩阵行和列名称
Eigen3: Matrix row and column names
本文关键字:特征 更新时间:2023-10-16
在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;
然后按键"草莓香蕉"或值 {0, 0} 进行搜索。
相关文章:
- 如何声明特征矩阵,然后通过嵌套循环初始化它
- 特征::矩阵<双精度,1,3> 结构类型函数中的返回类型函数
- 有没有一种方法可以通过"typedef"为重新定义的基本类型定义特征和强制转换运算符
- 特征命名访问向量段
- 将特征矩阵的向量设置为0
- 特征:模板函数中矩阵的平面图
- basic_string的前导/尾部不区分空格的特征
- 特征 3 类的模板专用化
- 特征 c++:复矩阵的面积双曲正切(atanh)
- C++ 中的特征向量计算
- 根据C++标准的定义实现"is_similar"类型特征
- C++类型特征,以查看是否可以<uint32_t>对类型"K"的任何变量调用"static_cast(k)"
- 有没有办法找到特征矩阵系数的中值?
- 如何将高维数据映射到特征类型?
- 将平面阵列重塑为复杂的特征类型
- 特征 LLT 模块给出不正确的结果?
- 特征模板化函数和维度
- 特征稀疏向量:求最大系数
- 特征 3.3.x:如何在所有行中操作 lamba?
- 如何将向量断言到特征矩阵