How to copy a 2d matrix along higher dims e.g. 3rd up to 6th without using for loop?

3 ビュー (過去 30 日間)
nima
nima 2019 年 9 月 23 日
コメント済み: nima 2019 年 9 月 24 日
In general how we can copy an N-dim matrix along higher dims (N+1, N+2, ..) without for loop?
I'm sure we can use repmat to copy a 2D matrix a 3 times along the 3rd dim e.g. (a,[1 1 3]) but I don't know can it work for higher dims.
  2 件のコメント
madhan ravi
madhan ravi 2019 年 9 月 23 日
Why did you delete the previous question?
nima
nima 2019 年 9 月 23 日
So sorry. I think this question is more general that previous one to solve my problem.

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

採用された回答

Matt J
Matt J 2019 年 9 月 23 日
repmat can be used to copy along more than one dimension, e.g.,
>> repmat(3,4,5)
ans =
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
3 3 3 3 3
The real question is why you would want to do that. You would be consuming more memory without storing any new information. For most purposes, repmat can be avoided.
  3 件のコメント
Matt J
Matt J 2019 年 9 月 23 日
編集済み: Matt J 2019 年 9 月 23 日
repmat is not necessary for that. It is far more efficient to reshape this into a matrix-vector multiplication:
[L,M,N,P]=size(b);
B=reshape(b,L*M,N*P);
C=a(:).'*B;
c=reshape(C,L,M);
Note that this involves neither for-loops nor data copying (reshape does not copy any data).
nima
nima 2019 年 9 月 24 日
Oh. Thanks a lot Matt.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by