Matrix multiplication with logical values error

조회 수: 6 (최근 30일)
Camilo A.
Camilo A. 2016년 10월 31일
답변: Hans Ruder 2025년 6월 11일
I have this matrix:
M =
0 3 0 0
0 2 0 0
2 0 3 4
0 4 0 2
And I call these vectors:
I=~(any(3==M'))', J=~any(3==M)
I =
0
1
0
1
J =
1 0 0 1
Why I cant multiplicate "I*J"? And how can I fix it?
I*J
Error using *
Both logical inputs must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.
I want this result:
ans =
0 0 0 0
1 0 0 1
0 0 0 0
1 0 0 1
Thanks

채택된 답변

Massimo Zanetti
Massimo Zanetti 2016년 10월 31일
편집: Massimo Zanetti 2016년 10월 31일
For logical matrices matrix multiplication doesn't apply (because it is not logical operation). To get your Dyadic product, cast to double:
dyd = double(I)*double(J)
NOTE: matrix product is not fully supported for integer classes in MATLAB, so casting to integer types (to save memory) won't work too..

추가 답변 (1개)

Hans Ruder
Hans Ruder 2025년 6월 11일
Alternatively, you can simply do:
dyd = I & J
Then the result will keep the logical data type.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by