How to extend an array to a new dimension?

32 ビュー (過去 30 日間)
Sufayan Mulani
Sufayan Mulani 2023 年 8 月 29 日
回答済み: Star Strider 2023 年 8 月 29 日
Suppose, I have an array
a=rand(3, 4);
I want to create a new array 'b' which has [ 3, 4, 5] size, and all 2D matrix along the third dimension are equal to 'a'.
b = zeros([size(a) 5]);
for i=1:5
b(:, :, i) = a;
end
How can I do this using MATLAB functions.

採用された回答

Star Strider
Star Strider 2023 年 8 月 29 日
One option is the repmat function —
a=rand(3, 4);
b = zeros([size(a) 5]);
for i=1:5
b(:, :, i) = a;
end
b
b =
b(:,:,1) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b(:,:,2) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b(:,:,3) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b(:,:,4) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b(:,:,5) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327
b2 = repmat(a, 1, 1, 5)
b2 =
b2(:,:,1) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b2(:,:,2) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b2(:,:,3) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b2(:,:,4) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327 b2(:,:,5) = 0.2207 0.2683 0.7580 0.4175 0.6446 0.7519 0.0339 0.4267 0.9818 0.8499 0.6944 0.9327
Both results are the same.
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by