I found this question below which got some good answers:
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.