<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688</link>
    <title>MATLAB Central Newsreader - 2D Array to Index a 3D Array?</title>
    <description>Feed for thread: 2D Array to Index a 3D Array?</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by 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>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 21 Nov 2008 16:32:08 -0500</pubDate>
      <title>2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612409</link>
      <author>Chris Maryan</author>
      <description>Lets say I have a 3D array M (i.e. M(x,y,z))&lt;br&gt;
&lt;br&gt;
I also have a 2D array J(x,y), where each element contains the z index&lt;br&gt;
of interest in M. Can I use J to index into M without looping (i.e.&lt;br&gt;
with a matlab indexing trick of some sort)?&lt;br&gt;
&lt;br&gt;
i.e.&lt;br&gt;
M(:,:,1) = [1,2;3,4];&lt;br&gt;
M(:,:,1) = [21,22;23,24];&lt;br&gt;
&lt;br&gt;
J = [1,2; 2,1];&lt;br&gt;
&lt;br&gt;
D = M(:,:,J) % This doens't work, I'm looking for something that does&lt;br&gt;
ans = [1  22&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;23  4]&lt;br&gt;
&lt;br&gt;
Any ideas? Thanks,&lt;br&gt;
&lt;br&gt;
Chris</description>
    </item>
    <item>
      <pubDate>Fri, 21 Nov 2008 18:15:04 -0500</pubDate>
      <title>Re: 2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612439</link>
      <author>Doug Hull</author>
      <description>Chris,&lt;br&gt;
&lt;br&gt;
There is going to have to be some kind of loop in there.  Why are you trying to avoid them.  There once was a performance reason to avoid FOR loops, but that has been largely removed by the JIT.&lt;br&gt;
&lt;br&gt;
-Doug&lt;br&gt;
&lt;br&gt;
Chris Maryan &amp;lt;kmaryan@gmail.com&amp;gt; wrote in message &amp;lt;718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; Lets say I have a 3D array M (i.e. M(x,y,z))&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I also have a 2D array J(x,y), where each element contains the z index&lt;br&gt;
&amp;gt; of interest in M. Can I use J to index into M without looping (i.e.&lt;br&gt;
&amp;gt; with a matlab indexing trick of some sort)?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; i.e.&lt;br&gt;
&amp;gt; M(:,:,1) = [1,2;3,4];&lt;br&gt;
&amp;gt; M(:,:,1) = [21,22;23,24];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; J = [1,2; 2,1];&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; D = M(:,:,J) % This doens't work, I'm looking for something that does&lt;br&gt;
&amp;gt; ans = [1  22&lt;br&gt;
&amp;gt;          23  4]&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any ideas? Thanks,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Chris&lt;br&gt;
&amp;gt; </description>
    </item>
    <item>
      <pubDate>Fri, 21 Nov 2008 18:34:02 -0500</pubDate>
      <title>Re: 2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612446</link>
      <author>Doug Hull</author>
      <description>clear&lt;br&gt;
clc&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
M(:,:,1) = [1,2;3,4];&lt;br&gt;
M(:,:,2) = [21,22;23,24];&lt;br&gt;
&lt;br&gt;
J = [1,2; 2,1];&lt;br&gt;
&lt;br&gt;
%D = M(:,:,J) % This doens't work, I'm looking for something that does&lt;br&gt;
%ans = [1 22&lt;br&gt;
%      23 4]&lt;br&gt;
&lt;br&gt;
[nR, nC] = size(J);&lt;br&gt;
&lt;br&gt;
for r = 1:nR&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;for c = 1:nC&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;D(r,c) = M(r,c,J(r,c));&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;end&lt;br&gt;
end&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
&quot;Doug Hull&quot; &amp;lt;hull@mathworks.SPAMPROOFcom&amp;gt; wrote in message &amp;lt;gg6tr8$t1e$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Chris,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There is going to have to be some kind of loop in there.  Why are you trying to avoid them.  There once was a performance reason to avoid FOR loops, but that has been largely removed by the JIT.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; -Doug&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Chris Maryan &amp;lt;kmaryan@gmail.com&amp;gt; wrote in message &amp;lt;718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com&amp;gt;...&lt;br&gt;
&amp;gt; &amp;gt; Lets say I have a 3D array M (i.e. M(x,y,z))&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I also have a 2D array J(x,y), where each element contains the z index&lt;br&gt;
&amp;gt; &amp;gt; of interest in M. Can I use J to index into M without looping (i.e.&lt;br&gt;
&amp;gt; &amp;gt; with a matlab indexing trick of some sort)?&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; i.e.&lt;br&gt;
&amp;gt; &amp;gt; M(:,:,1) = [1,2;3,4];&lt;br&gt;
&amp;gt; &amp;gt; M(:,:,1) = [21,22;23,24];&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; J = [1,2; 2,1];&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; D = M(:,:,J) % This doens't work, I'm looking for something that does&lt;br&gt;
&amp;gt; &amp;gt; ans = [1  22&lt;br&gt;
&amp;gt; &amp;gt;          23  4]&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Any ideas? Thanks,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Chris&lt;br&gt;
&amp;gt; &amp;gt; </description>
    </item>
    <item>
      <pubDate>Fri, 21 Nov 2008 18:42:01 -0500</pubDate>
      <title>Re: 2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612450</link>
      <author>Bruno Luong</author>
      <description>Chris Maryan &amp;lt;kmaryan@gmail.com&amp;gt; wrote in message &amp;lt;718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Any ideas? Thanks,&lt;br&gt;
&lt;br&gt;
M(:,:,1) = [1,2;3,4];&lt;br&gt;
M(:,:,2) = [21,22;23,24];&lt;br&gt;
J = [1,2; 2,1];&lt;br&gt;
[I1 I2]=ndgrid(1:size(M,1),1:size(M,2));&lt;br&gt;
M(sub2ind(size(M),I1,I2,J))&lt;br&gt;
&lt;br&gt;
% Bruno</description>
    </item>
    <item>
      <pubDate>Fri, 21 Nov 2008 18:48:47 -0500</pubDate>
      <title>Re: 2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612452</link>
      <author>Walter Roberson</author>
      <description>Chris Maryan wrote:&lt;br&gt;
&amp;gt; Lets say I have a 3D array M (i.e. M(x,y,z))&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; I also have a 2D array J(x,y), where each element contains the z index&lt;br&gt;
&amp;gt; of interest in M. Can I use J to index into M without looping (i.e.&lt;br&gt;
&amp;gt; with a matlab indexing trick of some sort)?&lt;br&gt;
&lt;br&gt;
&amp;gt; i.e.&lt;br&gt;
&amp;gt; M(:,:,1) = [1,2;3,4];&lt;br&gt;
&amp;gt; M(:,:,1) = [21,22;23,24];&lt;br&gt;
&lt;br&gt;
I will presume that that should have been M(:,:,2) = &amp;lt;etc&amp;gt;&lt;br&gt;
&lt;br&gt;
&amp;gt; J = [1,2; 2,1];&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; D = M(:,:,J) % This doens't work, I'm looking for something that does&lt;br&gt;
&amp;gt; ans = [1  22&lt;br&gt;
&amp;gt;          23  4]&lt;br&gt;
&amp;nbsp;&lt;br&gt;
&amp;gt; Any ideas? Thanks,&lt;br&gt;
&lt;br&gt;
D = reshape(M((1:size(M,1)*size(M,2)).' + (J(:)-1)*size(M,1)*(size(M,2))),size(J))&lt;br&gt;
&lt;br&gt;
This can be written more clearly by introducing a temporary variable.&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
-- &lt;br&gt;
.signature note: I am now avoiding replying to unclear or ambiguous postings.&lt;br&gt;
Please review questions before posting them. Be specific. Use examples of what you mean,&lt;br&gt;
of what you don't mean. Specify boundary conditions, and data classes and value&lt;br&gt;
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?</description>
    </item>
    <item>
      <pubDate>Fri, 21 Nov 2008 22:53:02 -0500</pubDate>
      <title>Re: 2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612508</link>
      <author>Phil Goddard</author>
      <description>&lt;br&gt;
Another approach:&lt;br&gt;
&lt;br&gt;
D = M(reshape(1:numel(J),size(J))+numel(J)*(J-1));&lt;br&gt;
&lt;br&gt;
It's pretty hard to determine the most efficient approach without having a better idea of the true sizes of the matrices that you might be using.&lt;br&gt;
&lt;br&gt;
Phil. </description>
    </item>
    <item>
      <pubDate>Sat, 22 Nov 2008 04:04:58 -0500</pubDate>
      <title>Re: 2D Array to Index a 3D Array?</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/239688#612537</link>
      <author>ImageAnalyst</author>
      <description>On Nov 21, 11:32=A0am, Chris Maryan &amp;lt;kmar...@gmail.com&amp;gt; wrote:&lt;br&gt;
&amp;gt; Lets say I have a 3D array M (i.e. M(x,y,z))&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; I also have a 2D array J(x,y), where each element contains the z index&lt;br&gt;
&amp;gt; of interest in M. Can I use J to index into M without looping (i.e.&lt;br&gt;
&amp;gt; with a matlab indexing trick of some sort)?&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; i.e.&lt;br&gt;
&amp;gt; M(:,:,1) =3D [1,2;3,4];&lt;br&gt;
&amp;gt; M(:,:,1) =3D [21,22;23,24];&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; J =3D [1,2; 2,1];&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; D =3D M(:,:,J) % This doens't work, I'm looking for something that does&lt;br&gt;
&amp;gt; ans =3D [1 =A022&lt;br&gt;
&amp;gt; =A0 =A0 =A0 =A0 =A023 =A04]&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Any ideas? Thanks,&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Chris&lt;br&gt;
&lt;br&gt;
-----------------------------------------------------&lt;br&gt;
Chris:&lt;br&gt;
I know exactly what you want to do since I deal with large 3D images&lt;br&gt;
all the time.  However, if you're looking for a tricky MATLAB indexing&lt;br&gt;
trick, I think you are risking having your code be unintelligible by&lt;br&gt;
using some tricky code rather than just doing it using the more&lt;br&gt;
straightforward looping way (like Doug's way).  No offense to those&lt;br&gt;
compact, elegant MATLAB-specific methods, but if you ever have to try&lt;br&gt;
to figure out what you did months or years from now . . . well . . .&lt;br&gt;
you know what I mean.  I do looping over gigabyte-sized arrays in just&lt;br&gt;
a few minutes or less.  I'm not sure you would save much time by using&lt;br&gt;
tricky methods but the understandability of your code mgiht suffer.&lt;br&gt;
And I suspect some of those tricky commands might have looping - it's&lt;br&gt;
just hidden inside the function so you don't see it.  Sometimes I&lt;br&gt;
think it's better to have readable, understandable code that may be a&lt;br&gt;
bit longer than to have compact vectorized code that's difficult to&lt;br&gt;
understand.&lt;br&gt;
My two cents,&lt;br&gt;
ImageAnalyst</description>
    </item>
  </channel>
</rss>

