Path: news.mathworks.com!not-for-mail
From: "Per Sundqvist" <per.sundqvist@live.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to do vectorize this simple code?
Date: Sat, 15 Nov 2008 10:39:02 +0000 (UTC)
Organization: Chalmers Tekniska H&#246;gskola
Lines: 20
Message-ID: <gfm8s6$2lf$1@fred.mathworks.com>
References: <gfkstu$1id$1@fred.mathworks.com>
Reply-To: "Per Sundqvist" <per.sundqvist@live.com>
NNTP-Posting-Host: webapp-05-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226745542 2735 172.30.248.35 (15 Nov 2008 10:39:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Sat, 15 Nov 2008 10:39:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 266682
Xref: news.mathworks.com comp.soft-sys.matlab:500979


"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.

This works, but its not so obvious and simple as it should be

M=rand(4)
row=(1:3);
col=(2:4);
y=diag(M(row,col))
/Per