필터 지우기
필터 지우기

Matlab OR | operator not working

조회 수: 2 (최근 30일)
bsriv
bsriv 2019년 2월 21일
댓글: Steven Lord 2019년 2월 22일
Hi I have a dataset (x.data) of 90,000 or so indices with values at each index. I am trying to pull the indices at certain values 21009 and 21003
When I sum(x.data==21009) I get 80, which is correct (ie, there are 80 indices with values of 21009). When I sum(x.data==21003) I get 150, also expected.
However when I try to pull both sum(x.data== 21009 | 21003) I get 90,000 and when I look at the output for x.data==21009 | 21003) it's just a column of 1s. It's probably something obvious but what am I missing?
  댓글 수: 1
Stephen23
Stephen23 2019년 2월 22일
Following the documented rules of operator precedence
the syntax that you invented
x.data== 21009 | 21003
is equivalent to
(x.data== 21009) | 21003
which, because the term in the parentheses can be either false or true, is therefore equivalent either of these:
(false) | 21003
(true) | 21003
which, because any non-zero values is considered to be true, are equivalent to these:
(false) | true
(true) | true
which (using standard rules of logic) is clearly always true.

댓글을 달려면 로그인하십시오.

채택된 답변

Bob Thompson
Bob Thompson 2019년 2월 21일
To the best of my knowledge the or operator doesn't work quite like that. You need to set separate conditions on each side of the or, rather than multiple values to check for.
x.data == 21009 | x.data == 21003;
  댓글 수: 2
bsriv
bsriv 2019년 2월 21일
Thanks!
Steven Lord
Steven Lord 2019년 2월 22일
If you later decide that you want to select elements that match any of a larger set of values (3, 4, or more) consider switching to ismember instead of chaining together 3, 4, or more expressions with the or operator | between them.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by