1

I have a matrix which state (in each enterence (i,j)) the correlations of the (i,j) pair. I want the indexes of k pairs with maximal correlations. Any ideas? My matrix could get big so I am looking for the matlabs elegant array/matrix commands (if there is any)

Thanks!

remark: If this is not the right place for this question, I would be thankfull to get a link to another forum or an answer to a similar question in this forum

user135172
  • 283
  • 1
  • 9
  • try looking here: [http://stackoverflow.com/questions/2692482/get-the-indices-of-the-n-largest-elements-in-a-matrix](http://stackoverflow.com/questions/2692482/get-the-indices-of-the-n-largest-elements-in-a-matrix) – Binyamin Even Nov 27 '16 at 15:29

1 Answers1

1

This should do the trick:

function [values,i,j] = maxi(A,n)
[a, linIdx] = sort(A(:),'descend');
values = a(1:n);
[i,j] = ind2sub(size(A),linIdx(1:n));

where input A is a matrix, and n is the number of (i,j) pairs you want returned. Outputs are of course the n maximum values and their corresponding indices.

I hope this helps.

Note: If A contains a lot of zero elements, consider using sparse matrix to decrease computation time.

jkazan
  • 1,149
  • 12
  • 29
  • @user135172 : Note that I improved the sloppy code I submitted before. This will be faster, and the code looks much better. Enjoy ;) – jkazan Nov 27 '16 at 20:09