How to assemble 3D array from four vectors

1 回表示 (過去 30 日間)
Ranny Meier
Ranny Meier 2020 年 11 月 21 日
コメント済み: Ranny Meier 2020 年 11 月 22 日
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
the cyclist 2020 年 11 月 21 日
編集済み: the cyclist 2020 年 11 月 21 日
Here is one way:
G = reshape([FF; MF; FM; MM],2,2,6)
  3 件のコメント
the cyclist
the cyclist 2020 年 11 月 22 日
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 2020 年 11 月 22 日
Definitely not stupid - simply work in progress. Thank you.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by