Got Questions? Get Answers.
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:
convert long 2D matrix into a 3D matrix

Subject: convert long 2D matrix into a 3D matrix

From: Alex

Date: 11 Feb, 2011 21:46:05

Message: 1 of 4

I have a 50x2500 2D matrix that I want to turn into a 50x50x50 3D matrix. I have a method jerry-rigged in which I redefine my original 2D matrix, A, as C below by defining each matrix by hand:

C = mat2cell(A, [50], [50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50]);

Although this works, I am looking for a more compact and efficient method as I will be expanding my code to turn a 512x307200 2D matrix into a 512x512x600 3D matrix, or, perhaps larger. I don't really want to type out my code with 512 parts.

Thanks,
Alex

Subject: convert long 2D matrix into a 3D matrix

From: Sean de

Date: 11 Feb, 2011 21:55:05

Message: 2 of 4

"Alex " <blue.harvest.83@gmail.com> wrote in message <ij4amt$kpf$1@fred.mathworks.com>...
> I have a 50x2500 2D matrix that I want to turn into a 50x50x50 3D matrix. I have a method jerry-rigged in which I redefine my original 2D matrix, A, as C below by defining each matrix by hand:
>
> C = mat2cell(A, [50], [50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50 50]);
>
> Although this works, I am looking for a more compact and efficient method as I will be expanding my code to turn a 512x307200 2D matrix into a 512x512x600 3D matrix, or, perhaps larger. I don't really want to type out my code with 512 parts.
>
> Thanks,
> Alex

doc reshape
doc permute

Subject: convert long 2D matrix into a 3D matrix

From: Alex

Date: 17 Feb, 2011 23:47:04

Message: 3 of 4

'"Sean de"
>
> doc reshape
> doc permute\

Thanks for the help Sean, but I ended up getting it another way. I did it my taking apart the large 2D matrix and rebuilding it into the 3D one I was looking for with a loop.

clear all
clc
tic

A = TEXTREAD('3Dsampledata.asc');


for Z = 1:50:length(A)
    B(:,:,Z) = A(:,1:50);
    A(:,1:50) = [];
end


toc
beep


Only problem I have to deal with now is that instead of recalling then like B(:,:,1), B(:,:,2), B(:,:,3), ..., B(:,:,50) so on, I have to go B(:,:,1), B(:,:,51), B(:,:,101), ..., B(:,:,2451).

-Alex

Subject: convert long 2D matrix into a 3D matrix

From: Matt J

Date: 18 Feb, 2011 14:16:03

Message: 4 of 4

"Alex " <blue.harvest.83@gmail.com> wrote in message <ijkc1n$6ad$1@fred.mathworks.com>...
> '"Sean de"
> >
> > doc reshape
> > doc permute
>
> Thanks for the help Sean, but I ended up getting it another way. I did it my taking apart the large 2D matrix and rebuilding it into the 3D one I was looking for with a loop.
=================

Really not a good idea. It is vastly more efficient to do

B=reshape(A,50,50,[]);

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