フィルターのクリア

problem using matrix indexing with bsxfun.

4 ビュー (過去 30 日間)
Tristan
Tristan 2013 年 10 月 31 日
コメント済み: Tristan 2013 年 10 月 31 日
I'd like to calculate only A(a)*B(b), while keeping the original format of bsxfun(@times,A,B)
>> A=[-1 2 -3;4 -5 6;-7 8 -9];
B(:,:,1)=[-1 2 -3;4 -5 6;-7 8 -9];
B(:,:,2)=[-1 2 -3;4 -5 6;-7 8 -9];
bsxfun(@times,A,B)
ans(:,:,1) =
1 4 9
16 25 36
49 64 81
ans(:,:,2) =
1 4 9
16 25 36
49 64 81
>> a=A<0;b=B<0;
>> bsxfun(@times,A(a),B(b))
Error using bsxfun
Non-singleton dimensions of the two input arrays
must match each other.

採用された回答

Matt J
Matt J 2013 年 10 月 31 日
編集済み: Matt J 2013 年 10 月 31 日
I'd like to calculate only A(a)*B(b)
Have you checked what A(a) and B(b) look like? They are both vectors of different sizes so A(a)*B(b) has no clear definition,
>> A(a)
ans =
-1
-7
-5
-3
-9
>> B(b)
ans =
-1
-7
-5
-3
-9
-1
-7
-5
-3
-9
If you want all combinations of products A(a(i))*B(b(j)), you don't need bsxfun at all. It's just an outer product calculation,
A(a)*B(b).'
If this is not what you want, then you need to clarify what the final result should look like.
  3 件のコメント
Matt J
Matt J 2013 年 10 月 31 日
編集済み: Matt J 2013 年 10 月 31 日
I assume you know for your specific data that such a multi-dimensional reshaping will always be possible. If you're sure it will be, then you can do
Aa=A(A<0);
Bb=reshape(B(B<0),[],1,size(B,3));
bsxfun(@times,Aa,Bb)
Tristan
Tristan 2013 年 10 月 31 日
perfect, thanks.

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

その他の回答 (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