Suppose a large sparse adjacency matrix. I want two matrices:
- 1st value matrix where first column for the largest element, second column for the second largest element and third for the third largest element
- 2nd index matrix where first column for the index of the largest element, second column for the index of the second largest element and third for the index of the third largest element
where the partial sort per vector is probably most efficiently done with sort's partial directive, like here https://stackoverflow.com/a/2453619/164148.
Does there exist some ready solutions for the partial sort of matrices by its largest elements to corresponding value/index matrices?
max.col works for the maximum position while the order method below works for any number of maximal positions: we order the index matrix to descreasing order and pick the wanted count of largest positions. Similar to max.col, we could use which.max function
and repeatedly finding the maximal position until not needed. We will next concentrate on the order method. Alternative A shows a case where the value matrix and the index matrix are equal. Alternative B shows a case where the value matrix and the index matrix are not equal.
Alternative A showing a case with index matrix equal to the value matrix.
that is the index matrix and its value matrix, in this case, is the same.
Alternative B showing where the index matrix not equal to the value matrix.
max.col returns the maximum position of each row of a matrix, breaking ties at random, but it does not have the partial directive
and
where unfortunately partial directive is not defined in the max.col command. So we test the method A:
that is correct and other way around we get
and the corresponding value matrix
where the value matrix is not equal to the earlier index matrix.