# How to assemble 3D array from four vectors

1 view (last 30 days)
Ranny Meier on 21 Nov 2020
Commented: Ranny Meier on 22 Nov 2020
We have four vectors of complex numbers.
FF = 1 x N vector, FM = 1 x N vector, MF = 1 x N vector, MM = 1 x N vector
We need a 3D array G = 2 x 2 x N array, and where each page is [FF(n) FM(n); MF(n) MM(n)]
FF = [1+2i 2+2i 3+2i 4+2i 5+2i 6+2i];
FM = [7+2i 8+2i 9+2i 10+2i 11+2i 12+2i];
MF = [13+2i 14+2i 15+2i 16+2i 17+2i 18+2i];
MM = [19+2i 20+2i 21+2i 22+2i 23+2i 24+2i];
G = [FF(1) FM(1); MF(1) MM(1)];
for n = 2:6
G(:,:,n) = [FF(n) FM(n); MF(n) MM(n)];
end
G
We tried to assemble it with a reshape function after taking noncongugate transpose.
R = reshape([FF.' MF.'; FM.' MM.'].',2,2,[])
The result is the correct shape of 3D array, and yet it seems that the elements are mixed up.
Is there a better way to assemble this 3D array, other than adding a page at a time in a loop?

the cyclist on 21 Nov 2020
Edited: the cyclist on 21 Nov 2020
Here is one way:
G = reshape([FF; MF; FM; MM],2,2,6)

Ranny Meier on 22 Nov 2020
Thank you. Maybe we were close. Need each page arranged as: G(:,:,1) =
1.0000 + 2.0000i 7.0000 + 2.0000i
13.0000 + 2.0000i 19.0000 + 2.0000i
R = reshape([FF; MF; FM; MM],2,2,6)
R(:,:,1) =
1.0000 + 2.0000i 7.0000 + 2.0000i
13.0000 + 2.0000i 19.0000 + 2.0000i
Also your earlier suggestion that we found in the email is arranged as desired.
H = permute(reshape([FF; FM; MF; MM],2,2,6),[2 1 3 4 5 6])
H(:,:,1) =
1.0000 + 2.0000i 7.0000 + 2.0000i
13.0000 + 2.0000i 19.0000 + 2.0000i
I am reading permute description and have yet to see the magic of how a size 1x6 dimorder works on a size 2x2x6 array.
the cyclist on 22 Nov 2020
I postsed that first solution too fast, which is part of the reason I edited it quickly.
The first solution, which I edited away, should only have been:
H = permute(reshape([FF; FM; MF; MM],2,2,6),[2 1 3])
The other dimensions were stupidity, not magic. :-)
Then I realized that if I swapped FM and MF, then the permutation can be avoided, for a much cleaner solution.
Ranny Meier on 22 Nov 2020
Definitely not stupid - simply work in progress. Thank you.