How to assemble 3D array from four vectors

1 view (last 30 days)
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?

Accepted Answer

the cyclist
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)
  3 Comments
the cyclist
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
Ranny Meier on 22 Nov 2020
Definitely not stupid - simply work in progress. Thank you.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!