How to create a 5D matrix by repeating a 2D array?

Hello everyone,
I'm trying to create a 5D matrix from a 2D array by repeating it for each array in the 5D matrix. I managed to do it using for loop.
A = zeros(1,70); % 2D array
X = zeros(length(N),length(lg),length(C),length(E),length(A)); %% 5D matrix
for p = 1:1:length(A)
X(:,:,:,:,p)=A(p);
end
Is there any shorter way to do this?
Thanks.

 採用された回答

Fangjun Jiang
Fangjun Jiang 2020 年 7 月 27 日

0 投票

A has 70*70 elements. The loop runs only 70 times.
Anyway, look into repmat(), reshape(),repelem().

3 件のコメント

Ramadhan Hakim
Ramadhan Hakim 2020 年 7 月 27 日
編集済み: Ramadhan Hakim 2020 年 7 月 27 日
I'm sorry, I wrote the code wrong. A is actually 1x70 matrix. I edited it already.
Already tried the functions you mentioned but still I couldnt get the matrix that I wanted.
Fangjun Jiang
Fangjun Jiang 2020 年 7 月 27 日
編集済み: Fangjun Jiang 2020 年 7 月 27 日
A=1:5;
X=zeros(2,3,5);
X=reshape(repelem(A,2*3),2,3,[])
Ramadhan Hakim
Ramadhan Hakim 2020 年 7 月 28 日
Ok thanks, I'll try that.

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

その他の回答 (0 件)

カテゴリ

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

製品

Community Treasure Hunt

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

Start Hunting!

Translated by