Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Is an entry in a matrix largest, or second largest, or ...?

Subject: Is an entry in a matrix largest, or second largest, or ...?

From: David Epstein

Date: 29 Mar, 2012 17:38:12

Message: 1 of 4

Let M be a matrix of doubles, and m an entry of M. Then we can find the rank of m in M as sum(M>m) + sum(M==m)/2

What I want to do is to determine the rank of all entries of M. The only way I can think of using arrayfun is to have M as a global variable.

Or is there some better way? Loop??

Subject: Is an entry in a matrix largest, or second largest, or ...?

From: Matt J

Date: 29 Mar, 2012 17:49:12

Message: 2 of 4

"David Epstein" <David.Epstein.spam@remove.warwick.ac.uk> wrote in message <jl26m4$d38$1@newscl01ah.mathworks.com>...
> Let M be a matrix of doubles, and m an entry of M. Then we can find the rank of m in M as sum(M>m) + sum(M==m)/2
>
> What I want to do is to determine the rank of all entries of M. The only way I can think of using arrayfun is to have M as a global variable.
>
> Or is there some better way? Loop??

When not histogram M using HISTC or similar?

Subject: Is an entry in a matrix largest, or second largest, or ...?

From: David Epstein

Date: 29 Mar, 2012 19:15:18

Message: 3 of 4

"Matt J" wrote in message <jl27ao$f6s$1@newscl01ah.mathworks.com>...
> "David Epstein" <David.Epstein.spam@remove.warwick.ac.uk> wrote in message <jl26m4$d38$1@newscl01ah.mathworks.com>...
> > Let M be a matrix of doubles, and m an entry of M. Then we can find the rank of m in M as sum(M>m) + sum(M==m)/2
> >
> > What I want to do is to determine the rank of all entries of M. The only way I can think of using arrayfun is to have M as a global variable.
> >
> > Or is there some better way? Loop??
>
> When not histogram M using HISTC or similar?

Thanks. That works well because all the entries are integers. So, with histc and cumsum I should be able to do what I want.

Subject: Is an entry in a matrix largest, or second largest, or ...?

From: Roger Stafford

Date: 29 Mar, 2012 23:58:25

Message: 4 of 4

"David Epstein" <David.Epstein.spam@remove.warwick.ac.uk> wrote in message <jl26m4$d38$1@newscl01ah.mathworks.com>...
> Let M be a matrix of doubles, and m an entry of M. Then we can find the rank of m in M as sum(M>m) + sum(M==m)/2
> What I want to do is to determine the rank of all entries of M.
- - - - - - - - -
  An alternate method is to do two sorts on M. (This works even if the elements in M are not integers.)

 n = numel(M);
 [~,p1] = sort(M(:),'ascend');
 [~,p2] = sort(M(:),'descend');
 q1 = (1:n).'; q1(p1) = q1; % Get inverses of p1 and p2
 q2 = (1:n).'; q2(p2) = q2;
 r = (n+1-q1+q2)/2; % <-- The corresponding rankings for elements in M(:)

(I added 1/2 to your definition of "rank" because it seems "nicer". To get yours, remove the "+1" in the expression for 'r'.)

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us