Path: news.mathworks.com!not-for-mail
From: "Henry " <henry@fullspectrumengineering.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to do vectorize this simple code?
Date: Fri, 14 Nov 2008 23:36:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 32
Message-ID: <gfl212$m0t$1@fred.mathworks.com>
References: <gfkstu$1id$1@fred.mathworks.com> <gfl0tf$srs$1@fred.mathworks.com>
Reply-To: "Henry " <henry@fullspectrumengineering.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226705762 22557 172.30.248.37 (14 Nov 2008 23:36:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Fri, 14 Nov 2008 23:36:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1601498
Xref: news.mathworks.com comp.soft-sys.matlab:500927


Hmm all those suggestions don't seem to be very efficient compared to my original code of:
for i=1:num_elements
    y(i)=my_matrix(row(i),col(i));
end

I just thought I could write it as a single line like y=my_matrix(row,col) where row and col are vectors and it would match them up as pairs and return a vector.

Seems like a common procedure and I'm surprised matlab doesn't have it built in...

"Mahdieh" <mahdieh.emrani@capitalhealth.ca> wrote in message <gfl0tf$srs$1@fred.mathworks.com>...
> "Henry " <henry@fullspectrumengineering.com> wrote in message <gfkstu$1id$1@fred.mathworks.com>...
> > I want to vectorize this simple snippet:
> > 
> > for i=1:num_elements
> >     y(i)=my_matrix(row(i),col(i));
> > end
> > 
> > I tried y=my_matrix(row,col)
> > and y=my_matrix([row col])
> > 
> > but nothing seems to work.  Any pointers?  Thanks.
> 
> Hi Henry
> 
> I guess this works:
> 
> U = mymatrix(row,:);
> V = U(:,col);
> y = diag(V);
> 
> -Mah