- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Combine two masks with the logical_or operator in Numpy
To combine two masks with the logical_or operator, use the mask_or() method in Python Numpy. If copy parameter is False and one of the inputs is nomask, return a view of the other input mask. Defaults to False. The shrink parameter suggests whether to shrink the output to nomask if all its values are False. Defaults to True. The function returns the result masks values that are masked in either mask1 or mask2. The result may be a view on mask1 or mask2 if the other is nomask (i.e. False).
Steps
At first, import the required library −
import numpy as np import numpy.ma as ma
Mask 1 and Mask 2 −
mask1 = np.ma.make_mask([0, 0, 1, 0, 0]) mask2 = np.ma.make_mask([0, 1, 0, 1, 0])
Display the masks −
print("Mask1...
", mask1) print("Mask2...
", mask2)
To combine two masks with the logical_or operator, use the mask_or() method in Python Numpy −
print("
Result...
",np.ma.mask_or(mask1, mask2))
Example
import numpy as np import numpy.ma as ma # Masks mask1 = np.ma.make_mask([0, 0, 1, 0, 0]) mask2 = np.ma.make_mask([0, 1, 0, 1, 0]) print("Mask1...
", mask1) print("Mask2...
", mask2) # To combine two masks with the logical_or operator, use the mask_or() method in Python Numpy print("
Result...
",np.ma.mask_or(mask1, mask2))
Output
Mask1... [False False True False False] Mask2... [False True False True False] Result... [False True True True False]
- Related Articles
- Combine two columns in R data frame with comma separation.
- Combine two arrays in C#
- Perform element-wise comparison of two string arrays using a comparison operator in Numpy
- Combine two different arrays in JavaScript
- Combine two MySQL fields and update a third one with result?
- What are subnets and subnet masks in computer networks?
- How to combine two vectors by separating with different special characters in R?
- Combine values of two columns separated with hyphen in an R data frame.
- Accumulate the result of applying the operator to all elements in Numpy
- How to combine two vectors while replacing the NA values with the values in the other vector in R?
- In MySQL, how can I combine two or more strings along with a separator?
- How to combine two tables and add a new column with records in MySQL?
- Return the inner product of two masked arrays with different shapes in Numpy
- Return the outer product of two masked arrays with different shapes in Numpy
- Convert inputs to arrays with at least two dimensions in Numpy

Advertisements