MATLAB: How to get the three maximum values from a vector

array

Hi, I want to know an efficient way of getting the three maximum values of a vector. Here is the whole question.
First I have a binary row vector of size n^2. This whole string can be split into substrings to form a matrix of order nxn. Then I sum the values of each column and it gives me a vector of size n of integers. for example, the string
0 0 1 0 1 0 – 0 0 0 1 1 1 -1 1 0 0 1 0 – 1 0 0 0 0 0 – 1 1 1 1 1 0
I used the next code to do the sum described above
for i=1:5
suma=0;j=i;
while(j<=5*5)
suma=prueba(j)+suma;
j=j+5;
end
vectorExito(i)=suma;
end
The vectorExito must be (3 2 2 2 4 1). I want to create a vector which has 1's in the positions that contains the three maximum values, otherwise 0's.
In this case I want the vector (1 1 1 1 1 0 ). Other example, if vectorExito is (2 3 4 4 5 1) then I want the vector (0 1 1 1 1 0). Other If vectorexito is (5 5 5 4 4 3) then the vector I want is ( 1 1 1 1 1 1).
How do I get the final binary vector given some vectorExito?

Best Answer

v=[5 2 4 4 1];
c=[0 0 unique(sort(v))];
res=zeros(1,length(v));
res(find(ismember(v,c(end-2:end))))=1