Problems repeating matrix in desired order

5 ビュー (過去 30 日間)
Erica McCune
Erica McCune 2017 年 8 月 11 日
コメント済み: Erica McCune 2017 年 8 月 17 日
Hi, I’m relatively new to Matlab and I’m incredibly stuck on how to do what I feel like should be a simple operation. I want to take a 3 dimensional matrix and repeat it a certain number of times to create a 4 dimensional matrix, but the repeat function is not organizing them the way I’d like and I cannot figure out a way to repeat the matrix in the desired order. For example, if I have a 3 dimensional matrix where (:,:,1) = [1 2; 3 4] and (:,:,2) = [5 6; 7 8], I want to create a 4 dimensional matrix where all the (:,:,1,1), (:,:,2,1), etc. data is the [1 2; 3 4] matrix and the (:,:,1,2),(:,:,2,2), etc. data is the [5 6; 7 8] matrix. Currently, however, what I get is a 4D matrix where (:,:,1,1) = [1 2;3 4] and (:,:,2,1) = [5 6; 7 8] and so on. Thanks in advance!

採用された回答

Stephen23
Stephen23 2017 年 8 月 11 日
編集済み: Stephen23 2017 年 8 月 11 日
A(:,:,1) = [1,2;3,4];
A(:,:,2) = [5,6;7,8];
N = 2;
repmat(permute(A,[1,2,4,3]),[1,1,N,1])
which gives exactly the output that you requested:
ans(:,:,1,1) =
1 2
3 4
ans(:,:,2,1) =
1 2
3 4
ans(:,:,1,2) =
5 6
7 8
ans(:,:,2,2) =
5 6
7 8
The permute rearranges the array so that the third dimension becomes the fourth dimension (which is where you want it in the output). The repmat is used to then repeat this new array N times along the third dimension.
  1 件のコメント
Erica McCune
Erica McCune 2017 年 8 月 17 日
This worked wonderfully, thank you!

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

その他の回答 (1 件)

Andrei Bobrov
Andrei Bobrov 2017 年 8 月 11 日
A(:,:,1) = [1 2; 3 4];
A(:,:,2) = [5 6; 7 8];
B = permute(repmat(A,1,1,1,2),[1 2 4 3]);

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by