Repeat elements of a vector as matrixes in a multidimensional array.

8 ビュー (過去 30 日間)
Idossou Marius Adom
Idossou Marius Adom 2019 年 12 月 2 日
Hello Everyone.
I want to repeat elements of a vector as matrixes in a multidimensional array. For example, say I have the vector v = [1 2 3 4 5]. Then I want to construct a three dimensional array w of dimension say 4*3*lenght(v) where: w(:,:,1) = v(1), w(:,:,2) = v(2), ... w(:,:,length(v)) = v(end). Is there a way to do this without using a for loop ? Any help would be precious.
Thank you.

採用された回答

Robert U
Robert U 2019 年 12 月 2 日
Hi Marius Adom,
If I understood your task correctly, you can use arrayfun() to create each x-y-Matrix and concatenate them using cat():
x = 4;
y = 3;
v = [1 2 3 4 5];
cOut = arrayfun(@(dIn,nInd) ones(x,y) .* dIn, v, 1:length(v), 'UniformOutput',false);
dOut = cat(3,cOut{:});
Kind regards,
Robert
  1 件のコメント
Idossou Marius Adom
Idossou Marius Adom 2019 年 12 月 2 日
Thank you very much. It works perfectly as I want it.
Regards.

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

その他の回答 (1 件)

James Tursa
James Tursa 2019 年 12 月 2 日
Another way:
m = size of 1st dimension
n = size of 2nd dimension
v = your row vector
result = reshape(repmat(v,m*n,1),m,n,[]);

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by