I use sparse matrices in COO format in my program. The COO format uses 3 separate vectors to represent the matrix: rowindex, colindex and values. I need to sort the matrix first by rowindex and then by colindex. For example, if the vectors contain:
rowindex = [1 2 2 1 0 2 0 1 0 2 1 2]
colindex = [7 7 2 1 3 9 8 6 6 0 3 4]
values = [0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1.0 1.1 1.2]
(meaning that element [1,7] in the matrix has a value of 0.1, element [2,7] has a value of 0.2, element [2,2] has a value of 0.3, etc) the matrix after sorting should be:
rowindex = [0 0 0 1 1 1 1 2 2 2 2 2]
colindex = [3 6 8 1 3 6 7 0 2 4 7 9]
values = [0.5 0.9 0.7 0.4 1.1 0.8 0.1 1.0 0.3 1.2 0.2 0.6]
I left some more spaces in the desired result to (hopefully) better show what I would like to achieve.
Can this be achieved somehow:
- Using the available sort functions in C++
- Without using additional memory (e.g. additional vectors), as the sparse matrices I use are huge and almost take up all memory
- Without having to resort to representing the matrix as an array of structs (where I know that the sort() function can be used).
Some answers I found about sorting multiple vectors, perform sorting regarding values of only one of the vectors. They do not have the requirement to sort elements that have the same value in the first vector, according to the second vector.