sortIdx matrix in both directions gone wrong
What I wish to do, is first sort the rows of the matrix and then sort the columns, so I get this result:
Input: randommatrix
[ 6, 197, 39, 29;
97, 110, 86, 193;
76, 129, 151, 138]
Output:
correctly sorted matrix
[ 6, 29, 39, 151;
76, 97, 110, 193;
86, 129, 138, 197]
Now I must use sortIdx instead of sort because I need to know the original column and row of each number. The sort method works fine. So what I did was:
sortIdx(matrix, matrixRowIndices, SORT_EVERY_ROW + SORT_ASCENDING);
I reconstruct the sorted matrix in two for loops based on those indices, giving Mat sortedRowmatrix. This prints correctly:
sorted row matrix =
[ 6, 29, 39, 197;
86, 97, 110, 193;
76, 129, 138, 151]
Now, I want to sort the columns. I use:
sortIdx(sortedRowmatrix, matrixColIndices, SORT_EVERY_COLUMN + SORT_ASCENDING);
I get the correct matrixColIndices (explanation below):
sorted col indices=
[0, 0, 0, 2;
2, 1, 1, 1;
1, 2, 2, 0]
since: Per column analysis of "sorted row matrix":
- 0th column: nr 0 (6) is the smallest, after that nr 2 (76), after that nr 1 (86)
- 1st column: nr 0 (29) is the smallest, nr 1 (97) after that, nr 2 (129) after that.
- 2nd column: nr 0 (39) is the smallest, nr 1 (110) after that, nr 2 (138) after that.
- 3rd column: nr 2 (151) is the smallest, nr 1 (193) after that en nr 0 (197) after that
Then, I write out my final matrix based on original indices and I expect to get the sorted result. Instead I get:
wrongly sorted matrix =
[ 6, 29, 39, 129;
151, 97, 110, 193;
97, 129, 138, 39]
Notice it also duplicates some numbers (97).
I cannot get my head around why my reconstruction apparently does not work. I sort horizontally: this gives me the correct column indices. I sort vertically: the horizontal ordning should stay the same, and this should give me correct row indices.
The normal sort method works (but of course does not give the original positions I need). I also tried sorting columns first, then rows. The first step sorting columns works. The second step again doesn't. What goes wrong?
My code is at: https://pastebin.com/0Li2j3xw