Dzeija
Dzeija

Reputation: 25

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.

Upvotes: 0

Views: 189

Answers (2)

Serge Steer
Serge Steer

Reputation: 446

You can use the gsort function:

 [S,ind]=gsort(distA,"g","d");

The index of the 4 largest elements is the given by

 ind(1:4)

Upvotes: 2

Kartik Aggarwal
Kartik Aggarwal

Reputation: 24

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.

Upvotes: 1

Related Questions