I have a NumericMatrix m. Say m is (the elements in the square brackets are the dim names)
7 9 8
4 6 5
1 3 2
with column names = {"x", "z", "y"}, row names = {"z", "y", "x"}
I want the following output
1 2 3
4 5 6
7 8 9
with column names = {"x", "y", "z"}, row names = {"x", "y", "z"}
So what I want to do is the following -
- Sort elements of each row according to the column names
- Permute the rows such that their corresponding row names are sorted
Is there an easy way to do this in Rcpp for a general NumericMatrix?
This isn't necessarily the simplest approach, but it appears to work:
I used a
std::map<std::string, int>
for two reasons:map
s automatically maintain a sorted order based on their keys, so by using the dim names as keys, the container does the sorting for us.