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:
Simple 2D to 3D conversion

Subject: Simple 2D to 3D conversion

From: Ryan

Date: 11 Jan, 2013 03:08:07

Message: 1 of 3

I've searched the help files and forums but still can't figure this out.
It's probably some mix of reshape and permute that I can't get.
I think it should be easy though:

Given
M = [1 2 ; 3 4 ; 5 6 ; 7 8 ; 9 10 ; 11 12 ; 13 14 ; 15 16]

M =

     1 2
     3 4
     5 6
     7 8
     9 10
    11 12
    13 14
    15 16

Generate a 3D array A without loops:

A(:,:,1) =

     1 2
     3 4
     5 6
     7 8


A(:,:,2) =

     9 10
    11 12
    13 14
    15 16

The method should be applicable to larger arrays as well.

Thanks

Subject: Simple 2D to 3D conversion

From: Roger Stafford

Date: 11 Jan, 2013 03:42:08

Message: 2 of 3

"Ryan " <rjpatton314@gmail.com> wrote in message <kcnvmn$jfi$1@newscl01ah.mathworks.com>...
> Given
> M = [1 2 ; 3 4 ; 5 6 ; 7 8 ; 9 10 ; 11 12 ; 13 14 ; 15 16]
>
> M =
>
> 1 2
> 3 4
> 5 6
> 7 8
> 9 10
> 11 12
> 13 14
> 15 16
>
> Generate a 3D array A without loops:
>
> A(:,:,1) =
>
> 1 2
> 3 4
> 5 6
> 7 8
>
>
> A(:,:,2) =
>
> 9 10
> 11 12
> 13 14
> 15 16
- - - - - - - - - - -
 A = permute(reshape(M.',2,4,2),[2,1]);

Roger Stafford

Subject: Simple 2D to 3D conversion

From: Bruno Luong

Date: 11 Jan, 2013 07:08:08

Message: 3 of 3

Roger's solution might be modified to adapt arbitrary long matrix (#rows multiple of 4).
 
permute(reshape(M',2,4,[]),[2 1 3])

Bruno

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