<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/166445</link>
    <title>MATLAB Central Newsreader - Voxel loop optimisation</title>
    <description>Feed for thread: Voxel loop optimisation</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2008 by The MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>The MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Thu, 27 Mar 2008 02:13:02 -0400</pubDate>
      <title>Voxel loop optimisation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/166445#423050</link>
      <author>Sven </author>
      <description>Hi there, I've got a 3d matrix and I'm trying to select a&lt;br&gt;
subset of elements to create a 2d matrix. At the moment I'm&lt;br&gt;
doing this in a loop, and was wondering if anyone had a&lt;br&gt;
trick they could teach me to avoid this loop becoming a&lt;br&gt;
bottleneck.&lt;br&gt;
&lt;br&gt;
Imagine a 3x3x3 rubix cube of elements:&lt;br&gt;
&amp;gt;&amp;gt; rc = rand(3,3,3);&lt;br&gt;
&lt;br&gt;
I can select, say, a plane through the 3rd dimension as follows:&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; myPlanar2d = squeeze(rc(2,:,:));&lt;br&gt;
&lt;br&gt;
But what if I want to move along the 3rd dimension of my&lt;br&gt;
rubix cube and select, say, the [1 3 2] rows instead of just&lt;br&gt;
the second row as above? At the moment I'm doing this in a&lt;br&gt;
loop as follows:&lt;br&gt;
&lt;br&gt;
idxs = [1 3 2];&lt;br&gt;
myIndexed2d = zeros(3,3);&lt;br&gt;
for i = 1:length(idxs)&lt;br&gt;
&amp;nbsp;myIndexed2d(i,:) = rc(idxs(i),:,i);&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
Is there a shortcut I can take that avoids my loop?&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
Sven.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 27 Mar 2008 06:05:10 -0400</pubDate>
      <title>Re: Voxel loop optimisation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/166445#423062</link>
      <author>Phil Goddard</author>
      <description>You could use ARRAYFUN in conjunction with an appropriate &lt;br&gt;
function handle, i.e.&lt;br&gt;
&lt;br&gt;
fh = @(idx,page) rc(idx,:,page)';&lt;br&gt;
myIndexed2d = cell2mat(arrayfun(fh,idxs,1:length&lt;br&gt;
(idxs),'UniformOutput',false))';&lt;br&gt;
&lt;br&gt;
However, if you're looking for speed, then in newer &lt;br&gt;
versions of MATLAB (at least since since R2006a) the loop &lt;br&gt;
is likely to be faster for the purely numeric data that &lt;br&gt;
you're manipulating.&lt;br&gt;
&lt;br&gt;
Phil.&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Thu, 27 Mar 2008 14:39:07 -0400</pubDate>
      <title>Re: Voxel loop optimisation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/166445#423140</link>
      <author>Peter Boettcher</author>
      <description>"Sven " &amp;lt;sven.holcombe@gmail.deleteme.com&amp;gt; writes:&lt;br&gt;
&lt;br&gt;
&amp;gt; Hi there, I've got a 3d matrix and I'm trying to select a&lt;br&gt;
&amp;gt; subset of elements to create a 2d matrix. At the moment I'm&lt;br&gt;
&amp;gt; doing this in a loop, and was wondering if anyone had a&lt;br&gt;
&amp;gt; trick they could teach me to avoid this loop becoming a&lt;br&gt;
&amp;gt; bottleneck.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Imagine a 3x3x3 rubix cube of elements:&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; rc = rand(3,3,3);&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I can select, say, a plane through the 3rd dimension as follows:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt;&amp;gt;&amp;gt; myPlanar2d = squeeze(rc(2,:,:));&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; But what if I want to move along the 3rd dimension of my&lt;br&gt;
&amp;gt; rubix cube and select, say, the [1 3 2] rows instead of just&lt;br&gt;
&amp;gt; the second row as above? At the moment I'm doing this in a&lt;br&gt;
&amp;gt; loop as follows:&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; idxs = [1 3 2];&lt;br&gt;
&amp;gt; myIndexed2d = zeros(3,3);&lt;br&gt;
&amp;gt; for i = 1:length(idxs)&lt;br&gt;
&amp;gt;  myIndexed2d(i,:) = rc(idxs(i),:,i);&lt;br&gt;
&amp;gt; end&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Is there a shortcut I can take that avoids my loop?&lt;br&gt;
&lt;br&gt;
You can use sub2ind.  Build 3 3x3 matrices, where the first contains the&lt;br&gt;
i indices desired for each of the elements, the second the j indices, etc.&lt;br&gt;
&lt;br&gt;
lin_ind = sub2ind(size(rc), repmat(idxs.', 1, 3), repmat(1:3, 3, 1), repmat((1:3).', 1, 3));&lt;br&gt;
&lt;br&gt;
myIndexed2d = rc(lin_ind);&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
I leave the timing to you.  The loop will probably be faster, though&lt;br&gt;
sub2ind might win if you need to do this repeatedly, and precompute one&lt;br&gt;
or more of the index matrices.&lt;br&gt;
&lt;br&gt;
meshgrid or ndgrid can also help generate the "easy" matrices.  For&lt;br&gt;
instance, the second two matrices above (j and k) can be generated like:&lt;br&gt;
&lt;br&gt;
[j k] = ndgrid(1:3);&lt;br&gt;
&lt;br&gt;
-Peter&lt;br&gt;
</description>
    </item>
    <item>
      <pubDate>Tue, 01 Apr 2008 00:35:04 -0400</pubDate>
      <title>Re: Voxel loop optimisation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/166445#423881</link>
      <author>Sven </author>
      <description>"Phil Goddard" &amp;lt;philgoddardNOSPAM@telus.net&amp;gt; wrote in&lt;br&gt;
message &amp;lt;fsfdem$ait$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; You could use ARRAYFUN in conjunction with an appropriate &lt;br&gt;
&amp;gt; function handle, i.e.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; fh = @(idx,page) rc(idx,:,page)';&lt;br&gt;
&amp;gt; myIndexed2d = cell2mat(arrayfun(fh,idxs,1:length&lt;br&gt;
&amp;gt; (idxs),'UniformOutput',false))';&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; However, if you're looking for speed, then in newer &lt;br&gt;
&amp;gt; versions of MATLAB (at least since since R2006a) the loop &lt;br&gt;
&amp;gt; is likely to be faster for the purely numeric data that &lt;br&gt;
&amp;gt; you're manipulating.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Phil.&lt;br&gt;
&lt;br&gt;
Thanks Phil and Peter.&lt;br&gt;
The real voxel data I'm working with is much larger than&lt;br&gt;
3x3x3, so the sub2ind method ends up taking too much memory&lt;br&gt;
to build the index arrays.  The arrayfun method is new to me&lt;br&gt;
so I'll try this and check its speed compared to the loop.&lt;br&gt;
&lt;br&gt;
Thanks again,&lt;br&gt;
Sven.&lt;br&gt;
</description>
    </item>
  </channel>
</rss>
