0

I have the matrix:

A=[ 4 0 0 0 0
   0 3 0 0 0
   0 0 2 0 0
   0 0 0 1 0
   0 0 0 0 1 ];

I want to get the top k elements' values in the diag, such that the sum of these elements are larger than a predefined threshold T, whilst k is as large as possible.

For example: T= 0.9

            (4+3+2)/(trace(A)) = 0.818...
            (4+3+2+1)/(trace(A)) = 0.909...

i.e. we can get the vector of top values in A as: V= [4,3,2] and thus, k = 3.

Could anyone tell me how can I do that ?

kgk
  • 649
  • 1
  • 10
  • 23

1 Answers1

4

For the cumsum of diag(A) you want to find the last element where the comparison X./trace(A)<=T is true. Put that together into code:

find(cumsum(diag(A))./trace(A)<=T,1,'last')
Daniel
  • 36,610
  • 3
  • 36
  • 69
  • 1
    I think you have the right result, I'm just going to delete my answer, I was really confused...I guess you could add a sort descend before the cumsum to make sure the ordering. – GameOfThrows Mar 17 '16 at 10:15