Is it possible to do operations on 3D matrices without explicitly creating one?

1 回表示 (過去 30 日間)
Brando Miranda
Brando Miranda 2016 年 5 月 5 日
コメント済み: Brando Miranda 2016 年 5 月 5 日
This might sound as a weird question but consider the following code:
A = zeros(1,4,2);
delta = [-1 -1 -1 -1; 2 2 2 2];
W = ones(3,4);
A(1,:,:) = delta';
bsxfun(@times, W, A); % gives
The last line results in the correct result that I wanted/intended:
ans(:,:,1) =
-1 -1 -1 -1
-1 -1 -1 -1
-1 -1 -1 -1
ans(:,:,2) =
2 2 2 2
2 2 2 2
2 2 2 2
however, it seems really silly to me to have to create the temporary 3D matrix (3rd order tensor) to do this because delta *is* a 3D tensor (its just a 1x2x4 tensor). Using the fact that it is a 3D tensor, can we give it to bsxfun in order to treat it like the 3D tensor it is and give the above computation without creating that dummy variable that doesn't do anything?

回答 (1 件)

John D'Errico
John D'Errico 2016 年 5 月 5 日
編集済み: John D'Errico 2016 年 5 月 5 日
I think you are asking why did you need to create A? You don't. You could do this:
delta = [-1 -1 -1 -1; 2 2 2 2];
W = ones(3,4);
bsxfun(@times, W, reshape(delta.',[1,flip(size(delta))]))
It might be easier to read with an intermediate variable though.
  1 件のコメント
Brando Miranda
Brando Miranda 2016 年 5 月 5 日
I guess another thing that I was sort of worried is that delta might be in GPU so it seemed really silly to create new array just to add a dummy dimension that the 3D matrix/tensor already had. Does ur solution avoid that issue? (or maybe it doesn't matter)

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

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by