# Revision history [back]

Something similar to the following should work:

cv::Mat indices;
cv::sortIdx(matrix1, indices, cv::SORT_EVERY_ROW | cv::SORT_DESCENDING);

// I assume your matrix1 and matrix2 are of same size and of type float
// -> change it to your type
cv::Mat1f matrix1_sorted(1, matrix1.cols);
cv::Mat1f matrix2_sorted(1, matrix1.cols);
for( int i = 0; i < distances.cols; i++) {
matrix1_sorted(0,i) = matrix1(indices.at<int>(0, i));
matrix2_sorted(0,i) = matrix2(indices.at<int>(0, i));
}
// your matrices are now sorted according to matrix1


Please forgive me any errors, I wrote it from scratch without testing it.

Something similar to the following should work:

cv::Mat indices;
cv::sortIdx(matrix1, indices, cv::SORT_EVERY_ROW | cv::SORT_DESCENDING);

// I assume your matrix1 and matrix2 are of same size and of type float
// -> change it to your type
cv::Mat1f matrix1_sorted(1, matrix1.cols);
cv::Mat1f matrix2_sorted(1, matrix1.cols);
for( int i = 0; i < distances.cols; indices.cols; i++) {
matrix1_sorted(0,i) = matrix1(indices.at<int>(0, i));
matrix2_sorted(0,i) = matrix2(indices.at<int>(0, i));
}
// your matrices are now sorted according to matrix1


Please forgive me any errors, I wrote it from scratch without testing it.