Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

matlab - Reshape 3d matrix to 2d matrix

I have a 3d matrix (n-by-m-by-t) in MATLAB representing n-by-m measurements in a grid over a period of time. I would like to have a 2d matrix, where the spatial information is gone and only n*m measurements over time t are left (ie: n*m-by-t)

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need the command reshape:

Say your initial matrix is (just for me to get some data):

a=rand(4,6,8);

Then, if the last two coordinates are spatial (time is 4, m is 6, n is 8) you use:

a=reshape(a,[4 48]);

and you end up with a 4x48 array.

If the first two are spatial and the last is time (m is 4, n is 6, time is 8) you use:

a=reshape(a,[24 8]);

and you end up with a 24x8 array.

This is a fast, O(1) operation (it just adjusts it header of what the shape of the data is). There are other ways of doing it, e.g. a=a(:,:) to condense the last two dimensions, but reshape is faster.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...