
Re: second largest element in a matrix
Posted:
Apr 3, 2009 11:09 PM


Matt Fig wrote: > This may work though. The speed gain over the sorting is not tremendous because max is called twice. > > R = temp; % Preserve R. > R(~(R~=max(R))) = NaN; > [c,i] = max(R);
Depends on the matrix sizes. max() is an order(N) operation. sort is something like order (N*log(N)). With large matrices, two linear passes through the matrix would be considerably faster than a sort().
Note: in previous postings, someone posted a reference to a lineartime routine to find the "Q largest" values in a vector; that code would only pass through the matrix once (but might make up to Q comparisons at each point in order to establish the proper ordering.)

