From: Mike Bourassa on
I want to convert a 3D matrix to a 2D matrix.

Say I have a 3D matrix as follows:

a = rand(3,3,3)

a(:,:,1) =

0.4966 0.6449 0.3420
0.8998 0.8180 0.2897
0.8216 0.6602 0.3412


a(:,:,2) =

0.5341 0.8385 0.7027
0.7271 0.5681 0.5466
0.3093 0.3704 0.4449


a(:,:,3) =

0.6946 0.9568 0.1730
0.6213 0.5226 0.9797
0.7948 0.8801 0.2714

I would like to convert it to a 2D vertical concatenation of each 'slice' of the 3D matrix. So the result would be a matrix, b, of size 9x3:

b = [ 0.4966 0.6449 0.3420
0.8998 0.8180 0.2897
0.8216 0.6602 0.3412
0.5341 0.8385 0.7027
0.7271 0.5681 0.5466
0.3093 0.3704 0.4449
0.6946 0.9568 0.1730
0.6213 0.5226 0.9797
0.7948 0.8801 0.2714 ]

I can accomplish this with a loop and using 'vertcat'. Can this be done _without a loop_? I've tried 'reshape' and 'rot90' etc.

As always, any guidance or assistance is appreciated.
From: Yi Cao on
Try the following:

b = reshape(permute(a,[1 3 2]),[],3)

HTH
Yi

"Mike Bourassa" <bourassa-m(a)rmc.ca> wrote in message <i1sk25$9f7$1(a)fred.mathworks.com>...
> I want to convert a 3D matrix to a 2D matrix.
>
> Say I have a 3D matrix as follows:
>
> a = rand(3,3,3)
>
> a(:,:,1) =
>
> 0.4966 0.6449 0.3420
> 0.8998 0.8180 0.2897
> 0.8216 0.6602 0.3412
>
>
> a(:,:,2) =
>
> 0.5341 0.8385 0.7027
> 0.7271 0.5681 0.5466
> 0.3093 0.3704 0.4449
>
>
> a(:,:,3) =
>
> 0.6946 0.9568 0.1730
> 0.6213 0.5226 0.9797
> 0.7948 0.8801 0.2714
>
> I would like to convert it to a 2D vertical concatenation of each 'slice' of the 3D matrix. So the result would be a matrix, b, of size 9x3:
>
> b = [ 0.4966 0.6449 0.3420
> 0.8998 0.8180 0.2897
> 0.8216 0.6602 0.3412
> 0.5341 0.8385 0.7027
> 0.7271 0.5681 0.5466
> 0.3093 0.3704 0.4449
> 0.6946 0.9568 0.1730
> 0.6213 0.5226 0.9797
> 0.7948 0.8801 0.2714 ]
>
> I can accomplish this with a loop and using 'vertcat'. Can this be done _without a loop_? I've tried 'reshape' and 'rot90' etc.
>
> As always, any guidance or assistance is appreciated.
From: Jan Simon on
Dear Mike,

or less general:
a = rand(3,3,3)
b = [a(:,:,1); a(:,:,2); a(:,:,3)]
Jan
From: Mike Bourassa on
lol I had thought of the 'less general approach' but it doesn't scale well. Thank you all again for the assist.
 | 
Pages: 1
Prev: MATLAB Mex Error
Next: Find text