Path: news.mathworks.com!not-for-mail
From: "helper " <spamless@nospam.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: multidimensional indexing
Date: Tue, 13 May 2008 05:14:04 +0000 (UTC)
Organization: Timothy S. Farajian, Inc.
Lines: 27
Message-ID: <g0b82s$cbl$1@fred.mathworks.com>
References: <g0b6eb$18j$1@fred.mathworks.com>
Reply-To: "helper " <spamless@nospam.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 1210655644 12661 172.30.248.37 (13 May 2008 05:14:04 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Tue, 13 May 2008 05:14:04 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1272923
Xref: news.mathworks.com comp.soft-sys.matlab:468056


"Darius " <martin@econ.ucsb.edu> wrote in message 
<g0b6eb$18j$1@fred.mathworks.com>...
> Hi,
> 
> I'm looking for a sort of generalization of diag for 
> multidimensional arrays.  Specifically suppose I have:
> 
> A(:,:,1) = [1 2 3; 1 2 3; 1 2 3];
> 
> A(:,:,2) = [4 5 6; 4 5 6; 4 5 6];
> 
> A(:,:,3) = [7 8 9; 7 8 9; 7 8 9];
> 
> Is there a quick way to get
> 
> B = [1 5 9; 1 5 9; 1 5 9], so the first column of B is 
the 
> first column of the first page, the second column is the 
> second column of the second page, etc? (avoiding for 
> loops) Thanks!
> 

One of many methods:

i = repmat((1:size(A,1)),size(A,1),1);
I = sub2ind(size(A),i.',i,i);
A(I)