Path: news.mathworks.com!not-for-mail
From: "Sven " <sven.holcombe@gmail.deleteme.com>
Newsgroups: comp.soft-sys.matlab
Subject: Voxel loop optimisation
Date: Thu, 27 Mar 2008 02:13:02 +0000 (UTC)
Organization: The MathWorks, Inc.
Lines: 28
Message-ID: <fsevre$cg7$1@fred.mathworks.com>
Reply-To: "Sven " <sven.holcombe@gmail.deleteme.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 1206583982 12807 172.30.248.35 (27 Mar 2008 02:13:02 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Thu, 27 Mar 2008 02:13:02 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1326470
Xref: news.mathworks.com comp.soft-sys.matlab:459389



Hi there, I've got a 3d matrix and I'm trying to select a
subset of elements to create a 2d matrix. At the moment I'm
doing this in a loop, and was wondering if anyone had a
trick they could teach me to avoid this loop becoming a
bottleneck.

Imagine a 3x3x3 rubix cube of elements:
>> rc = rand(3,3,3);

I can select, say, a plane through the 3rd dimension as follows:

>> myPlanar2d = squeeze(rc(2,:,:));

But what if I want to move along the 3rd dimension of my
rubix cube and select, say, the [1 3 2] rows instead of just
the second row as above? At the moment I'm doing this in a
loop as follows:

idxs = [1 3 2];
myIndexed2d = zeros(3,3);
for i = 1:length(idxs)
 myIndexed2d(i,:) = rc(idxs(i),:,i);
end

Is there a shortcut I can take that avoids my loop?

Thanks,
Sven.