How to convert 3D matrix to 2D with a specific format

Hello all :)
I have a [1000,64,10] matrix and want to change it to a [10000,64] in a way that the first 1000 rows in 2D matrix belong to [:,:,1] in 3D matrix and the 2nd 1000 rows to [:,:,2] and so on, in other words I want the element (1001,1) in the new matrix corresponds to (1,1,2) in the old one and (2001,1) corresponds to (1,1,3) and so on.
Is there any function applying this rule?
Thanks :)

Answers (2)

A=rand(1000,64,10);
B=permute(A,[ 2 1 3]);
out=B(:,:)'

5 Comments

Dear Azzi
Thanks for the quick response!
I saw the same approach in the forum else where and if I'm not mistaken it was you who had recommended it, so I checked the "permute" function and given the examples I thought it just reorders dimensions while keeping the total dimension unchanged.
Now it seems that my reasoning was wrong, would you please clarify how your script converts [1000,64,10] to [10000,64] by the specific format I requested?
Cheers
Let us take short example
A(:,:,1)=[1 2;3 4;5 6];
A(:,:,2)=[10 20;30 40;50 60]
A(:,:,3)=[100 200 ;300 400;500 600]
A(:,:,1) =
1 2
3 4
5 6
A(:,:,2) =
10 20
30 40
50 60
A(:,:,3) =
100 200
300 400
500 600
B=permute(A,[ 2 1 3])
B(:,:,1) =
1 3 5
2 4 6
B(:,:,2) =
10 30 50
20 40 60
B(:,:,3) =
100 300 500
200 400 600
B(:,:)'
ans =
1 2
3 4
5 6
10 20
30 40
50 60
100 200
300 400
500 600
RH
RH on 5 Mar 2014
Edited: RH on 5 Mar 2014
Dear Azzi
Thanks again :)
So what I had neglected was that you transposesd the 3D array * (:,:) *, such a useful application to know :D
Cheers
RH, I posted a short example, I think my code is doing what you asked. If not, please, post the expected result from my short example
Dear Azzi
It does exactly what I was looking for... Thanks again! :)
Chears
A = randi(100,3,4,4);
a1 = num2cell(A,[1 2]);
out = cat(1,a1{:});
or
out = reshape(permute(A,[1 3 2]),[],size(A,2));

1 Comment

Dear Andrei
Thanks for the help, I'm going to check it too, it's always handy to know different solutions. :)
Cheers

This question is closed.

Asked:

RH
on 4 Mar 2014

Closed:

on 20 Aug 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!