Duplicate every element of a matrix to NxN elements

1 回表示 (過去 30 日間)
Xinbing Liu
Xinbing Liu 2019 年 2 月 14 日
コメント済み: Xinbing Liu 2019 年 2 月 14 日
Hello, I need to duplicate all the elements of an MxM matrix to NxN elements so the resulting matrix has the size (M*N)x(MxN). For example,
A =
1 2
3 4
I want the matrix B to be duplicated from A's elements such that
B =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
I have been using the following loops to accomplish this:
for k = 1:M
for j =1:M
B((j-1)*N+1 : j*N, (k-1)*N+1 : k*N) = A(j,k);
end
end
My question is: is there a more efficient way of doing it? If M and N are not large, it's not a big deal. But when they are large, the nested loop can take quite a long time.

採用された回答

Stephen23
Stephen23 2019 年 2 月 14 日
>> M = [1,2;3,4];
>> kron(M,[1,1;1,1])
ans =
1 1 2 2
1 1 2 2
3 3 4 4
3 3 4 4
  1 件のコメント
Xinbing Liu
Xinbing Liu 2019 年 2 月 14 日
Thank you very much. This solution works very well.

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

その他の回答 (1 件)

KSSV
KSSV 2019 年 2 月 14 日
A = [1 2
3 4 ] ;
[nx,ny] = size(A) ;
B = cell(nx,ny) ;
for i = 1:nx
for j = 1:ny
B{i,j} = repelem(A(i,j),nx,ny) ;
end
end
B = cell2mat(B)

カテゴリ

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

製品


リリース

R2016b

Community Treasure Hunt

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

Start Hunting!

Translated by