Search code examples
matlabthreshold

Pick up values in vector by a threshold


I have a vector

E = [ 2.91082 , 0.14735, 0.92122, 0.02061 ]

Now, I set the threshold T=0.95 which means that

T = ( 2.91082 + 0.92122)/(2.91082 + 0.14735 + 0.92122 + 0.02061 )=0.958>0.95

And then, I can pick up E[1] and E[3] as selected values.

Could you please tell me how can I do that ?


Solution

  • Use bsxfun for a vectorized solution -

    [R,C] = find(triu(bsxfun(@plus,E,E.')./sum(E),1) > 0.95)
    

    Sample run -

    >> E = [ 2.91082 , 0.14735, 0.92122, 0.02061 ];
    >> triu(bsxfun(@plus,E,E.')./sum(E),1) '%// T values
    ans =
                0      0.76454      0.95801      0.73286
                0            0      0.26714      0.04199
                0            0            0      0.23546
                0            0            0            0
    >> [R,C] = find(triu(bsxfun(@plus,E,E.')./sum(E),1) > 0.95)
    R =
         1
    C =
         3