From: davidebacc Bacchini on
The sort works good, but i have computational problems because I work with big vectors (1700 elements). I just need to find the 10 greatest numbers, not to sort all elements
Any other ideas?
From: Jos (10584) on
"davidebacc Bacchini" <davidebacc(a)hotmail.com> wrote in message <hjp60a$apc$1(a)fred.mathworks.com>...
> The sort works good, but i have computational problems because I work with big vectors (1700 elements). I just need to find the 10 greatest numbers, not to sort all elements
> Any other ideas?

1700 used to be big on a 1K machine back in the 70's ....

Although sorting is the way to go, here are two other options, which are unlikely to be faster ...

% some test data
V = randperm(100) ;
N = 5 ; % N largest elements

% option 1
R1 = repmat(-Inf,1,N) ; % result
for ii=1:numel(V),
D = V(ii) - R1 ;
if any(D > 0)
% V(ii) is larger than any element found so far
[unused,k] = max(D) ;
R1(k) = V(ii) ;
end
end
disp(R1) ;

% option 2
Vcopy = V ;
R2 = zeros(1,N) ;
for ii=1:N,
[R2(ii), idx] = max(Vcopy) ;
Vcopy(idx) = -Inf ;
end
disp(R2) ;

hth
Jos
From: Oleg Komarov on
"davidebacc Bacchini"
> The sort works good, but i have computational problems because I work with big vectors (1700 elements). I just need to find the 10 greatest numbers, not to sort all elements
> Any other ideas?

Are you the same Bacchini from Liceo scientifico Volterra?
BTW 1700 elements are nothing, what kind of computational problems you have?

Oleg
From: Bruno Luong on
"davidebacc Bacchini" <davidebacc(a)hotmail.com> wrote in message <hjoq5i$34f$1(a)fred.mathworks.com>...
> Hello,
> what i need is to extract the indixes of the n greatest elements of a vector. Is there a function to do it?

This is the function (mex required)
http://www.mathworks.com/matlabcentral/fileexchange/23576-minmax-selection

Bruno
From: Steven Lord on

"davidebacc Bacchini" <davidebacc(a)hotmail.com> wrote in message
news:hjp60a$apc$1(a)fred.mathworks.com...
> The sort works good, but i have computational problems because I work with
> big vectors (1700 elements). I just need to find the 10 greatest numbers,
> not to sort all elements
> Any other ideas?

Sort once and use the sorted vector repeatedly.

x = rand(1, 10);
y = [3 5 1 8 2 4];
S = sort(x);
for k = 1:numel(y)
disp(S(1:y(k)))
end

If you need to know where the elements were located in the original vector
x, use the second output from SORT in the same way.

--
Steve Lord
slord(a)mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ