
Re: second largest element in a matrix
Posted:
Apr 3, 2009 7:58 PM


"Paul " <pswirhun@comcast.net> wrote in message > if temp is your vector, you can get the second largest element as follows: > > [c,i] = max(temp(temp~=max(temp))) > why would you sort it? > this is the whole point of logical addressing in matlab.
% Some Data temp = [8 1 0 1 2 3 8 0 5 4]
% Proposed method [c,i] = max(temp(temp~=max(temp)));
% As a check: [srt,idx] = sort(temp,'descend'); idx2 = find(srt~=srt(1),1); secondhighest = srt(idx2); location = idx(idx2);
% isequal? [secondhighest==c,location==i]
Have I misunderstood your proposal?

