Path: news.mathworks.com!newsfeed-00.mathworks.com!news.kjsl.com!newsfeed.stanford.edu!elk.ncren.net!newsflash.concordia.ca!canopus.cc.umanitoba.ca!not-for-mail
From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Newsgroups: comp.soft-sys.matlab
Subject: Re: how to make the following case vectorized?
Date: Wed, 4 Jun 2008 20:02:08 +0000 (UTC)
Organization: National Research Council Canada - Conseil national de rechereches Canada
Lines: 29
Message-ID: <g26sc0$2vn$1@canopus.cc.umanitoba.ca>
References: <g26qnb$4nt$1@fred.mathworks.com>
NNTP-Posting-Host: origin.ibd.nrc.ca
X-Trace: canopus.cc.umanitoba.ca 1212609728 3063 192.70.172.160 (4 Jun 2008 20:02:08 GMT)
X-Complaints-To: abuse@cc.umanitoba.ca
NNTP-Posting-Date: Wed, 4 Jun 2008 20:02:08 +0000 (UTC)
Originator: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)
Xref: news.mathworks.com comp.soft-sys.matlab:472227



In article <g26qnb$4nt$1@fred.mathworks.com>,
James Anderson <janderson_net@yahoo.com> wrote:

>I have a matrix (say 10000 rows and 50 columns). I want to
>find the rank for each entry within the row it belongs to. I
>know how to do this by using a loop, suppose A is the matrix

>for k = 1:10000;
>  [sortx,ind] = sort(A(k,:));
>  rankt(ind) = 1:50;
>  Rank(k,:) = rankt;
>end

>The Rank contains what I need. However, this is going to
>take a long time, especially when the number of rows get
>even larger. I found the function sort can sort the whole
>matrix along one direction, however, is there any vectorized
>way to get the Rank very fast without doing any looping? 

You can do it as a two-step process:

[ind,ind] = sort(A,2);
[Rank,Rank] = sort(ind,2);

(The repeating of the variables on the left hand side is a signal
to Matlab that the value of the first return parameter is to be
discarded.)
-- 
  "And that's the way it is."                 -- Walter Cronkite