English 中文(简体)
c++:特征库新手排序
原标题:c++: Eigen Library newbie sort

我一辈子都想不出为什么这不正确。它似乎没有返回第k个元素。

typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic> matrix;

double test(matrix& D, int k)
{
    au到 d = D.row(1);
    std::nth_element(d.data(),d.data()+k, d.data()+d.size());
    return d(k) ;
}

我也试过

template <typename ScalarType, typename Derived>
void Sort(Eigen::MatrixBase<Derived> &xValues)
{
std::sort(xValues.derived().data(), xValues.derived().data()+xValues.derived().size());
}


double test(matrix& D, int k)
{
    au到 d = D.row(1);
    Sort<double>(d);
    return d(k) ;
}

非常感谢您的帮助。

编辑

我刚试着改变

au到 d = D.row(1);

Eigen::Vec到rXd rowD = D.row(1);
....

and it seems 到 work ok.

对此有点困惑。

最佳回答

特征矩阵是column-major默认值。这意味着矩阵的一行不是连续的C数组,并且不能将数据指针用作迭代器。

例如,3x4矩阵将被存储为:

0 3 6  9
1 4 7 10
2 5 8 11

现在,行(1)将是

1 4 7 10

但是您传递给nth_element()的指针迭代器将访问

1 2 3 4

如果您将矩阵typedef更改为row major,则代码有效:

typedef Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> matrix;

更新:由于将行复制到向量中,因此您编辑的示例可以正常工作。对于向量(一维矩阵),数据是以行为主还是以列为主存储并不重要。

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?