Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
2D Array to Index a 3D Array?

Subject: 2D Array to Index a 3D Array?

From: Chris Maryan

Date: 21 Nov, 2008 16:32:08

Message: 1 of 7

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) = [1,2;3,4];
M(:,:,1) = [21,22;23,24];

J = [1,2; 2,1];

D = M(:,:,J) % This doens't work, I'm looking for something that does
ans = [1 22
         23 4]

Any ideas? Thanks,

Chris

Subject: 2D Array to Index a 3D Array?

From: Doug Hull

Date: 21 Nov, 2008 18:15:04

Message: 2 of 7

Chris,

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.

-Doug

Chris Maryan <kmaryan@gmail.com> wrote in message <718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com>...
> 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) = [1,2;3,4];
> M(:,:,1) = [21,22;23,24];
>
> J = [1,2; 2,1];
>
> D = M(:,:,J) % This doens't work, I'm looking for something that does
> ans = [1 22
> 23 4]
>
> Any ideas? Thanks,
>
> Chris
>

Subject: 2D Array to Index a 3D Array?

From: Doug Hull

Date: 21 Nov, 2008 18:34:02

Message: 3 of 7

clear
clc


M(:,:,1) = [1,2;3,4];
M(:,:,2) = [21,22;23,24];

J = [1,2; 2,1];

%D = M(:,:,J) % This doens't work, I'm looking for something that does
%ans = [1 22
% 23 4]

[nR, nC] = size(J);

for r = 1:nR
    for c = 1:nC
        D(r,c) = M(r,c,J(r,c));
    end
end


"Doug Hull" <hull@mathworks.SPAMPROOFcom> wrote in message <gg6tr8$t1e$1@fred.mathworks.com>...
> Chris,
>
> 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.
>
> -Doug
>
> Chris Maryan <kmaryan@gmail.com> wrote in message <718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com>...
> > 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) = [1,2;3,4];
> > M(:,:,1) = [21,22;23,24];
> >
> > J = [1,2; 2,1];
> >
> > D = M(:,:,J) % This doens't work, I'm looking for something that does
> > ans = [1 22
> > 23 4]
> >
> > Any ideas? Thanks,
> >
> > Chris
> >

Subject: 2D Array to Index a 3D Array?

From: Bruno Luong

Date: 21 Nov, 2008 18:42:01

Message: 4 of 7

Chris Maryan <kmaryan@gmail.com> wrote in message <718c2ac0-bc91-4be9-8fc5-5a4fbd364596@s20g2000yqh.googlegroups.com>...

>
> Any ideas? Thanks,

M(:,:,1) = [1,2;3,4];
M(:,:,2) = [21,22;23,24];
J = [1,2; 2,1];
[I1 I2]=ndgrid(1:size(M,1),1:size(M,2));
M(sub2ind(size(M),I1,I2,J))

% Bruno

Subject: 2D Array to Index a 3D Array?

From: Walter Roberson

Date: 21 Nov, 2008 18:48:47

Message: 5 of 7

Chris Maryan 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) = [1,2;3,4];
> M(:,:,1) = [21,22;23,24];

I will presume that that should have been M(:,:,2) = <etc>

> J = [1,2; 2,1];
 
> D = M(:,:,J) % This doens't work, I'm looking for something that does
> ans = [1 22
> 23 4]
 
> Any ideas? Thanks,

D = reshape(M((1:size(M,1)*size(M,2)).' + (J(:)-1)*size(M,1)*(size(M,2))),size(J))

This can be written more clearly by introducing a temporary variable.


--
.signature note: I am now avoiding replying to unclear or ambiguous postings.
Please review questions before posting them. Be specific. Use examples of what you mean,
of what you don't mean. Specify boundary conditions, and data classes and value
relationships -- what if we scrambled your data or used -Inf, NaN, or complex(rand,rand)?

Subject: 2D Array to Index a 3D Array?

From: Phil Goddard

Date: 21 Nov, 2008 22:53:02

Message: 6 of 7


Another approach:

D = M(reshape(1:numel(J),size(J))+numel(J)*(J-1));

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.

Phil.

Subject: 2D Array to Index a 3D Array?

From: ImageAnalyst

Date: 22 Nov, 2008 04:04:58

Message: 7 of 7

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

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us