필터 지우기
필터 지우기

How to take logical or of all the elements in the vector without a loop?

조회 수: 3 (최근 30일)
Jay Vaidya
Jay Vaidya 2020년 11월 16일
댓글: KSSV 2020년 11월 16일
I am trying to use
or(a(1,1):a(1,size(a,2)))
but it doesn't run for more than 2 elements.
also when I run
or(1,1,0,0.1,0,1)
it doesn't run. It only runs for 2 inputs i.e.
or(1,0)
Is there no direct way that I can take the logical OR of elements a(1,1) to a(1,size(a,2))?

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 11월 16일
편집: Ameer Hamza 2020년 11월 16일
or() is an operator just defined for two inputs. In case of multiple inputs, you can use any() which is equivalent to or of all elements
>> y = any([1,1,0,0,1,0,1])
y =
logical
1
Similarly, all() is equivalent to and of all elements
>> y = all([1,1,0,0,1,0,1])
y =
logical
0

추가 답변 (1개)

KSSV
KSSV 2020년 11월 16일
Note that you have two types of indexings in MATLAB.
  1. Indexing
This is a number and should be posittive integers greater than 0. If number is negative or fracation, it will throw error.
A = rand(1,10) ;
A(1) % first element
A(end) % last element
A(1:5) % elements from 1 to 5
A(-) % error
A(0) % error
2. Logical indexing
Here indices are 0 and 1's. This would be class of logical.
A = rand(1,10) ;
idx = A>0.5 ; % get elements greater than 0.5. 0 where conditon fails and 1 where condition mets.
A(idx) % this will pick the elements at the above condition satisfies
  댓글 수: 4
Jay Vaidya
Jay Vaidya 2020년 11월 16일
편집: Jay Vaidya 2020년 11월 16일
Hello, probably I did not make it clear enough.
I want the ans = a(1,1) | a(1,2) | a(1,3) | a(1,4) incase of a 4 element vector.
In other words, I need to OR the elements
like 1 | 1 | 0 | 1 | 0 | 1 | 0 | 0.
where v is the boolean OR operator.
KSSV
KSSV 2020년 11월 16일
a = [1 0 1 1 0 1 0 0 0 1 1 0 0 1 1 1 1 1 0 0 1] ;
b = a(1)|a(2) ;
for i = 3:length(a)
b = b|a(i) ;
end

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by