How to take logical or of all the elements in the vector without a loop?
Show older comments
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))?
1 Comment
KSSV
on 16 Nov 2020
Is a a matrix? What exactly you are looking for?
Accepted Answer
More Answers (1)
KSSV
on 16 Nov 2020
Note that you have two types of indexings in MATLAB.
- 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 Comments
Jay Vaidya
on 16 Nov 2020
KSSV
on 16 Nov 2020
idx = a | ~a ;
Jay Vaidya
on 16 Nov 2020
Edited: Jay Vaidya
on 16 Nov 2020
KSSV
on 16 Nov 2020
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
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!