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:
Extracting a matrix from a 3D array using an index matrix

Subject: Extracting a matrix from a 3D array using an index matrix

From: Joao Ejarque

Date: 9 Jun, 2011 11:05:05

Message: 1 of 4

I have two 3D arrays, C andY, both of size (n1,n2,n3).
I create [Vy Iy]=max(Y,[],3), where Iy is a matrix of indices of size (n1,n2).
I want to extract the two dimensional D=C(Iy), size (n1,n2), where the elements of this new matrix D are the elemets of C that have the same position along dimension 3 as the elements of Vy.
What is te simplest way to do it?

Thanks

Joao

Subject: Extracting a matrix from a 3D array using an index matrix

From: Roger Stafford

Date: 9 Jun, 2011 15:54:04

Message: 2 of 4

"Joao Ejarque" <jme@dreammodel.dk> wrote in message <isq9d1$2pd$1@newscl01ah.mathworks.com>...
> I have two 3D arrays, C and Y, both of size (n1,n2,n3).
> I create [Vy Iy]=max(Y,[],3), where Iy is a matrix of indices of size (n1,n2).
> I want to extract the two dimensional D=C(Iy), size (n1,n2), where the elements of this new matrix D are the elemets of C that have the same position along dimension 3 as the elements of Vy.
> What is te simplest way to do it?
>
> Thanks
>
> Joao
- - - - - - - - - - - -
 [I,J] = ndgrid(1:n1,1:n2);
 D = reshape(C(sub2ind(size(Y),I(:),J(:),IY(:))),n1,n2);

Roger Stafford

Subject: Extracting a matrix from a 3D array using an index matrix

From: Matt J

Date: 9 Jun, 2011 19:08:04

Message: 3 of 4

Another option:


idx=reshape( (1:n1*n2).' + (Iy(:)-1)*n3 , n1,n2 );
D=C(idx);

Subject: Extracting a matrix from a 3D array using an index matrix

From: Matt J

Date: 9 Jun, 2011 19:33:04

Message: 4 of 4

"Matt J" wrote in message <isr5mk$ro2$1@newscl01ah.mathworks.com>...
> Another option:
>
>
> idx=reshape( (1:n1*n2).' + (Iy(:)-1)*n3 , n1,n2 );
> D=C(idx);


make that

p=n1*n2;
idx=reshape( (1:p).' + (Iy(:)-1)*p , n1,n2 );
D=C(idx);

Tags for this Thread

No tags are associated with 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