"Oluwa KuIse" <email@example.com> wrote in message <firstname.lastname@example.org>... > Hello, > Can anyone pls tell me how I can find the second (or third) largest element in a matrix AND its position. The max function gives the largest element and its position but how do I find the second largest element and its position?
There are 2 rudimentary ways that I can think of off the top of my head...none of them are optimal though..
1. Use max to find the largest element, then set this element to -Inf. Then use max again to find the 'next'(ie, second) largest element and its position.
This method is definitely only a quick heck, and not suitable for looking for the Nth largest element in case N in large.
2. Sort the matrix, then you will know the value of the second largest element, then use 'find' to get the position of this element.