Error in Logical Array multiplication

2 ビュー (過去 30 日間)
Nathan
Nathan 2017 年 5 月 4 日
編集済み: dpb 2017 年 5 月 4 日
I found this error in my code and I don't think it should be an error. Anyone know if this is a bug or has MatLab programmed things this way for some reason?
K>> a=true(5,1);
K>> b=true(5,1);
K>> a*b(1,1);
Error using *
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.
K>> c=rand(5,1)
K>> d=rand(5,1)
K>> c*d(1,1)
K>> ver
MATLAB Version: 8.3.0.532 (R2014a)
MATLAB License Number: STUDENT
Operating System: Mac OS X Version: 10.9.5 Build: 13F1911
...[elided for brevity, of no import on question -- dpb]...

回答 (2 件)

dpb
dpb 2017 年 5 月 4 日
It is not a Matlab bug; it is owing to the definition of matrix multiplication not making sense for logical variables; the logical only makes sense on an element-wise calculation.

Star Strider
Star Strider 2017 年 5 月 4 日
Numerical multiplication is analogous to logical and, so you can do this:
a = true(5,1);
b = true(5,1);
c = a & b(1,1);

カテゴリ

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

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by