Vectorized operations on multidimensional arrays

3 ビュー (過去 30 日間)
Mikhail
Mikhail 2012 年 2 月 7 日
編集済み: Image Analyst 2013 年 10 月 2 日
Hi,
Say, there is 3D array a:
a(:,:,1) =
56 123
48 242
a(:,:,2) =
206 30
188 26
a(:,:,3) =
251 138
167 8
And 3D array m:
m(:,:,1) =
117.2500
m(:,:,2) =
112.5000
m(:,:,3) =
141
How do I subtract m(:,:,1:3) from a(:,:,1:3) in a vectorized manner? I.e. do something like
for k=1:length(a), res(:,:,k)=a(:,:,k)-m(:,:,k); end
The result should be:
res(:,:,1) =
-61.2500 5.7500
-69.2500 124.7500
res(:,:,2) =
93.5000 -82.5000
75.5000 -86.5000
res(:,:,3) =
110 -3
26 -133
Thank you!

採用された回答

Andrei Bobrov
Andrei Bobrov 2012 年 2 月 7 日
res = bsxfun(@minus,a,m)
please read about function bsxfun
  3 件のコメント
Andrei Bobrov
Andrei Bobrov 2012 年 2 月 7 日
res = bsxfun(@minus,a,reshape(m,1,1,[]))
or
res = bsxfun(@minus,a,permute(m,[1 3 2]))
please read:
http://www.mathworks.com/help/techdoc/ref/reshape.html
http://www.mathworks.com/help/techdoc/ref/permute.html
Mikhail
Mikhail 2012 年 2 月 7 日
Thanks again!

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

その他の回答 (0 件)

カテゴリ

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