Path: news.mathworks.com!newsfeed-00.mathworks.com!nlpi057.nbdc.sbc.com!prodigy.net!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!j38g2000yqa.googlegroups.com!not-for-mail
From: ImageAnalyst <imageanalyst@mailinator.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: 2D Array to Index a 3D Array?
Date: Fri, 21 Nov 2008 20:04:58 -0800 (PST)
Organization: http://groups.google.com
Lines: 40
Message-ID: <a1a20223-888b-45d9-8313-9b3cee5d6beb@j38g2000yqa.googlegroups.com>
References: <718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com>
NNTP-Posting-Host: 75.186.70.56
Mime-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
X-Trace: posting.google.com 1227326698 28631 127.0.0.1 (22 Nov 2008 04:04:58 GMT)
X-Complaints-To: groups-abuse@google.com
NNTP-Posting-Date: Sat, 22 Nov 2008 04:04:58 +0000 (UTC)
Complaints-To: groups-abuse@google.com
Injection-Info: j38g2000yqa.googlegroups.com; posting-host=75.186.70.56; 
	posting-account=0rLUzAkAAABojYSRC64DkTbtiSCX77HH
User-Agent: G2/1.0
X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET 
	CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.648; .NET CLR 
	3.5.21022),gzip(gfe),gzip(gfe)
Bytes: 2904
Xref: news.mathworks.com comp.soft-sys.matlab:502462


On Nov 21, 11:32=A0am, Chris Maryan <kmar...@gmail.com> wrote:
> Lets say I have a 3D array M (i.e. M(x,y,z))
>
> I also have a 2D array J(x,y), where each element contains the z index
> of interest in M. Can I use J to index into M without looping (i.e.
> with a matlab indexing trick of some sort)?
>
> i.e.
> M(:,:,1) =3D [1,2;3,4];
> M(:,:,1) =3D [21,22;23,24];
>
> J =3D [1,2; 2,1];
>
> D =3D M(:,:,J) % This doens't work, I'm looking for something that does
> ans =3D [1 =A022
> =A0 =A0 =A0 =A0 =A023 =A04]
>
> Any ideas? Thanks,
>
> Chris

-----------------------------------------------------
Chris:
I know exactly what you want to do since I deal with large 3D images
all the time.  However, if you're looking for a tricky MATLAB indexing
trick, I think you are risking having your code be unintelligible by
using some tricky code rather than just doing it using the more
straightforward looping way (like Doug's way).  No offense to those
compact, elegant MATLAB-specific methods, but if you ever have to try
to figure out what you did months or years from now . . . well . . .
you know what I mean.  I do looping over gigabyte-sized arrays in just
a few minutes or less.  I'm not sure you would save much time by using
tricky methods but the understandability of your code mgiht suffer.
And I suspect some of those tricky commands might have looping - it's
just hidden inside the function so you don't see it.  Sometimes I
think it's better to have readable, understandable code that may be a
bit longer than to have compact vectorized code that's difficult to
understand.
My two cents,
ImageAnalyst