0

I found this question below which got some good answers:

For example, I have a set, or vector, or matrix of samples A : [5, 2, 1, 4, 3]. I want to sort these to be B : [1,2,3,4,5], but I also want to remember the original indexes of the values, so I can get another set which would be: C : [2, 1, 4, 3, 0 ] - which corresponds to the index of the each element in 'B', in the original 'A'.

what if i extend the problem to 2 indices, say for example a 2x3 matrix

1 5 6

4 8 2

sorted (in descending, neither columnwise/rowwise, by values only)

8 6 5 4 2 1

output indices

(1,1) (0,2) (0,1) (1,0) (1,2) (0,0)

i tried but im sure i needed help and any help would mean so much...

template <typename T> vector <vector <size_t>> sort_indexes(const vector <vector <T>> &v)

//// initialize original index locations
vector<vector<size_t>> idx(v.size());
for (size_t i = 0; i != idx.size(); ++i) {
    idx[i] = i;
    for (size_t j = 0; i != idx[i].size(); ++j) {
        idx[j] = j;
    }
}
// sort indexes based on comparing values in v
sort(idx.begin(), idx.end(),
    [&v](size_t i1, size_t i2) {return v[i1] > v[i2]; });

return idx;

}

int main (){

vector<vector<int>> v;
v[0].push_back(10);
v[0].push_back(1);
v[0].push_back(3);
v[1].push_back(20);
v[1].push_back(30);
v[1].push_back(40);

for (auto i = 0; i < v.size(); i++) {
    for (auto j:sort_indexes(v) )
    cout << i << j <<"  "<< v[i][j] << endl;
}
cin.get();
return 0;

}

i adopted this https://stackoverflow.com/a/12399290/5807825 for my problem.i tried some tweaks on the code, can anyone shed some light please which parts are wrong, or everything...i hope not.

Community
  • 1
  • 1
roenan
  • 11
  • 1
  • did not really get your question, are you looking for an algorithm to implement it. the explanation is clear. do you just want to implement it, regardless of the process involve – Ibukun Muyide Jan 19 '16 at 01:01
  • thanks very much for asking. yes, i need a C++ code to implement it...answer from " Using C++ lambda function" from Lukasz Wiklendt was somehow gave me something to start with. i was trying his answer for my case but havent been successful. – roenan Jan 19 '16 at 10:00

1 Answers1

0

Generate an array of pointers or indices to the array, and sort the pointers or indices according to the array. If using pointers, the compare function just needs to know the type of elements in the array. If using indices, then a lambda compare can be used (assuming a language like C++, Java, ...).

Example C++ code for array using indices and lambda compare:

void examplesort(int array[], int index[], int size){
    int * tmpa = new int[size];     // temp array
    // generate indices to array
    for(int i = 0; i < size; i++)
        index[i] = i;
    // sort indices according to array
    std::sort(index, index+size,
        [&array](int i, int j) {return array[i] < array[j];});
    // tmpa[] = sorted array[]
    for(int i = 0; i < size; i++)
        tmpa[i] = array[index[i]];
    // copy tmpa to array
    for(int i = 0; i < size; i++)
        array[i] = tmpa[i];
    delete[] tmpa;
    return;
}
rcgldr
  • 27,407
  • 3
  • 36
  • 61
  • since i just jump into C++ vector/STL, i may have need time to figure out your suggections...But i have tried the way from @Lukasz Wiklendt, his answer of the question i referenced above? it works well for single index. Could you please show me some tweakings of his elaborate code that can handle the example i show above? – roenan Jan 19 '16 at 12:46