Search code examples
matlabscilab

How do I save the indices of the top 4 maximum numbers in a matrix in scilab


I need to save the indices of 4 maximum numbers for example, I need to get the indices of rows 10,9,7,5

5.0259327
4.7127487
4.8435524
4.8538644
5.1048996
6.2441973
5.9413803
6.2912638
5.1117512
5.8309519
5.7419509
6.9663477
5.9958319
6.9519781
6.5802736
6.7327558
7.6765878

I have used

[mA,nA]=max(distA) 
where mA is the row and nA is the column

in getting one maximum number but I cannot figure out how to choose another maximum number without duplication. I cannot sort because I need the indices.


Solution

  • You can use this little trick.

     [output_val, output_index] = max(input_mat(input_mat < max(input_mat)))
    

    This will give you the value and index of the second largest element. And then similarly, you can do it for 4 numbers.