johnny_b_good
johnny_b_good

Reputation: 41

Sorting RowVectorXd and return indices in new vector

I'm trying to order a RowVector by absolute value and return the indices corresponding to the unordered vector in descending order. SO if I have: x_rcv = (-2.5,3.9,1.5) the oei (indices vector) should read: oei = (2,1,3)

So what I always get is a oei = (0,0,0,0,0,0,0)

OEI.cpp

#include "OEI.h"
#include <eigen3/Eigen/Dense>
#include <iostream>
#include <algorithm>

void OEI::calculateOEI(RowVectorXd &v)
{

    std::sort(oei.data(), oei.data() + oei.size(), [&v](int i1, int i2)
              { return abs(v[i1]) < abs(v[i2]); });
    
    std::cout << oei.size() << std::endl;
}

OEI.h

#include <eigen3/Eigen/Dense>
#include <iostream>
#include <algorithm>
using namespace Eigen;

class OEI
{
public:
    int dim;
    OEI(int dim) : dim(dim) { oei = RowVectorXd::Zero(dim); };
    RowVectorXd oei;

public:
    void calculateOEI(RowVectorXd &v);
    
};

main.cpp

int main(){

Encode vec(4, 4, 7);
    awgn channel(7);
    OEI errorvec(7);

    vec.encodeDataVector();
    cout << vec.x << endl;
    channel.addGausian(vec.x);
    cout << channel.x_rcv << endl;
    errorvec.calculateOEI(channel.x_rcv);
    cout << errorvec.oei << endl;

}

I posted only the revelant code snippets which are not working. Let me know if you need more information.

Thank you!

Upvotes: 0

Views: 241

Answers (1)

user17537755
user17537755

Reputation: 161

The vector oei need to contain every indices:

The constructor should be:

OEI(int dim) : dim(dim) { 
    oei = RowVectorXd::Zero(dim);
    for (int i=0; i<dim; ++i) oei[i] = i;
};

To sort in descending order, the sort comparator should be greator than:

std::sort(oei.data(), oei.data() + oei.size(), [&v](int i1, int i2)
          { return abs(v[i1]) > abs(v[i2]); });

Upvotes: 2

Related Questions