I have an array dimensioned A(m,m,M,M). I would like to "reshape" this so that each mxm block is in the correct position in a B(m*M,m*M) matrix where the correct position is indicated by the third and fourth indices.
Can someone show me a straightforward way to do this?

 採用された回答

Stephen23
Stephen23 2025 年 2 月 21 日
編集済み: Stephen23 2025 年 2 月 21 日

1 投票

A = randi(9,3,3,5,5);
[~,M,~,N] = size(A);
B = permute(A,[1,3,2,4]);
B = reshape(B,[M*N,M*N])
B = 15×15
3 1 4 2 7 7 4 7 8 5 7 2 4 7 7 1 9 1 2 2 5 9 8 2 5 6 1 7 8 3 7 6 2 3 4 3 6 8 9 5 7 5 2 7 6 4 1 9 6 4 3 2 9 1 9 4 8 7 1 6 1 2 7 3 1 8 9 9 4 2 5 7 2 9 5 8 5 8 8 3 5 8 6 4 1 4 3 8 3 9 2 6 7 3 2 2 5 7 9 4 2 2 5 9 3 9 4 4 2 7 3 8 8 9 7 3 2 9 8 7 7 2 3 2 9 7 2 4 6 7 7 9 3 6 8 5 5 4 1 9 5 4 1 5 2 1 4 3 8 5
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Checking some random block:
X = 3;
Y = 5;
A(:,:,X,Y)
ans = 3×3
5 9 3 9 8 7 3 6 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B(...
((X-1)*M+1):(X*M),...
((Y-1)*M+1):(Y*M))
ans = 3×3
5 9 3 9 8 7 3 6 8
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

1 件のコメント

Bill Greene
Bill Greene 2025 年 2 月 21 日
Fantasitic, thanks!

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

その他の回答 (1 件)

Matt J
Matt J 2025 年 2 月 23 日
編集済み: Matt J 2025 年 2 月 23 日

0 投票

Download this File Exchange package,
and do the whole thing in one line,
B=blkReshape(A,[m,m],[M,M]);

カテゴリ

ヘルプ センター および File ExchangeMultidimensional Arrays についてさらに検索

製品

リリース

R2024b

タグ

質問済み:

2025 年 2 月 21 日

編集済み:

2025 年 2 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by