有没有更好的方法可以使用特征/C++实现 matlab 的逻辑索引?
Is there a better way to implement matlab's Logical Indexing using Eigen/C++?
我一直在寻找一种更"特征"的方式来实现 Matlab 逻辑索引的功能。这是我能想到的最好的。(为简单起见,这里重点介绍 int 数组(
//an attempt at matlab-style Logical Indexing
//equivalent to the matlab:
// original = [1,2,3,4]
// subset = original(original < 3)
using namespace Eigen;
using std::cout;
using std::endl;
IOFormat OctaveFmt(StreamPrecision, 0, ", ", " ", "", "", "[", "]");
ArrayXi original(4);
original << 1,2,3,4;
cout<<"Original with bad values:"<<endl
<<original.format(OctaveFmt)<<endl;
Array<bool, Dynamic,1> selections = original < 3;
cout<<"One if it's a good value:"<<endl
<<selections.format(OctaveFmt)<<endl;
std::vector<int> picked;
for(int i = 0; i < selections.size(); i++ )
{
if(selections(i))
{
picked.push_back(original(i));
}
}
//put the vector values back into an eigen array
ArrayXi theGoodStuff = Map<ArrayXi, Unaligned>
(picked.data(), picked.size());
cout<<"Just the good stuff:"<<endl
<<theGoodStuff.format(OctaveFmt)<<endl;
这是我得到的输出:
Original with bad values:
[1 2 3 4]
One if it's a good value:
[1 1 0 0]
Just the good stuff:
[1 2]
有谁知道如何以更"特征"的方式做到这一点,或者只是比循环遍历数组更快的方式?
这是一个旧的,但是....Eigen 3.4 使此操作更加干净。可悲的是,我无法与表演交谈。我主要对可读性感兴趣。我做了这个:
class logical
{
private:
const Index new_size;
Array<Index, Dynamic, 1> old_inds;
public:
logical(const Array<bool, Dynamic, 1> &keep) : new_size(keep.count()), old_inds(new_size)
{
for (Index i = 0, j = 0; i < keep.size(); i++)
if (keep(i))
old_inds(j++) = i;
}
Index size() const { return new_size; }
Index operator[](Index new_ind) const { return old_inds(new_ind); }
};
其用法如下:
logical inds(ind_to_keep);
Y_slice = Y(inds); // vector
H_slice = H(inds, all); // remove some rows
R_slice = R(inds, inds); // remove the same rows as columns
特征帮助位于本页底部: https://eigen.tuxfamily.org/dox-devel/group__TutorialSlicingIndexing.html
相关文章:
- 有没有更好的方法可以使用特征/C++实现 matlab 的逻辑索引?
- 为什么用于阈值矩阵元素的 Matlab 逻辑索引操作在性能上优于 mex 实现?
- 反向 Cuthill-McKee 算法:在 Matlab 和提升实现中具有无与伦比的结果
- 如何实现MATLAB和单独的C++应用程序之间的通信
- 在 Matlab 中非递归实现 perms,与 Coder 兼容
- 在C 或MATLAB中用于图像的CRF实现的代码
- 如何在 MATLAB 中实现/使用C++头文件
- 编码器在Matlab上实现C++、sym和perms函数
- 用C++实现Matlab语法
- 在C++中实现 Matlab 的 eps(x) 函数
- matlab函数fzero的C/C++实现
- matlab在opencv中查找函数的实现
- 使用Java实现服务器应用程序和Matlab客户端之间的套接字通信
- 尝试使用opencv在C++中实现matlab代码的一小部分
- 实现A(:,k)=b;c++矩阵库中的类matlab语法:避免const-ness
- 实现A(:,k)=b;一个c++矩阵库中的类似matlab的语法
- 实现 Matlab 的冒号:表达式模板类中的运算符C++
- 是否有任何现有的库在C/CPP/Obj-C中实现matlab函数
- Matlab中矩阵逆函数的c++实现
- 在c++中实现Matlab Interp2d