How do I select elements of an array given condition?
Your expression works if you add parentheses:
>>> y[(1 < x) & (x < 5)]
array(['o', 'o', 'a'],
dtype='|S1')
Selecting elements from an array based on a condition
Since you included Numpy in your tags, I'm assuming you're OK with a solution with Numpy?
import numpy as np
a = np.array([0,10,20])
np.min(a[a > 0])
Out[1]: 10
How do I select elements of an two array given condition?
Use numpy.where
:
np.where(y, x2, x1)
Output:
array([ 5, 2, 22, 1, 0, 7])
How to conditionally select elements in numpy array
You can index directly like:
sampleArr[sampleArr > 0.5]
Test Code:
sampleArr = np.array([0.725, 0.39, 0.99])
condition = (sampleArr > 0.5)
extracted = np.extract(condition, sampleArr) # returns [0.725 0.99]
print(sampleArr[sampleArr > 0.5])
print(sampleArr[condition])
print(extracted)
Results:
[ 0.725 0.99 ]
[ 0.725 0.99 ]
[ 0.725 0.99 ]
Assign new value to a condition selected element of an array
As others mentioned in their answers Yr[Xg>5]
is a new array and any change on it does not apply on the original array, but you can do it by using np.put
like this:
np.put(Yr, np.where(Xg>5)[0][2], 10)
Or simply do this:
Yr[np.where(Xg>15)[0][2]] = 10
Numpy: Selecting Rows based on Multiple Conditions on Some of its Elements
Elegant is np.equal
Z[np.equal(Z[:, [0,1]], 1).all(axis=1)]
Or:
Z[np.equal(Z[:,0], 1) & np.equal(Z[:,1], 1)]
JavaScript - get array element fulfilling a condition
In most browsers (not IE <= 8) arrays have a filter
method, which doesn't do quite what you want but does create you an array of elements of the original array that satisfy a certain condition:
function isGreaterThanFive(x) {
return x > 5;
}
[1, 10, 4, 6].filter(isGreaterThanFive); // Returns [10, 6]
Mozilla Developer Network has a lot of good JavaScript resources.
how to select values from two arrays according to a value condition from another array?
You can create an array with your conditions and use it to select from a
and c
.
import matplotlib.pyplot as plt
select = (b == 0) & (a > 0)
plt.plot(a[select], c[select])
plt.xlabel('a')
plt.ylabel('c');
Output
Selecting elements of a vector for which a matrix satisfies a condition
What you can do is create an "elimination array". Loop over all x,y
pairs, check the corresponding entry in S
and set elimination to true
if the condition is fulfilled. Then, at the end, remove all elements which should be eliminated.
x=[1,3,2,3];
y=[2,4,1,3];
S=[1,3,4,5;1,3,4,5;1,2,0,0.001;12,21,2,5];
% Preallocate to no elimination taking place
eliminate = zeros(size(x),'logical');
for ii = 1:numel(x) % Loop over all elements
if S(x(ii),y(ii)) < 0.1 % If the condition holds
eliminate(ii) = true; % Set elimination to true
end
end
% Remove elements to be eliminated
x(eliminate) = []
y(eliminate) = []
x =
1 2
y =
2 1
A one-liner to do this a bit more elegant
eliminate = diag(S(x,y))<0.1;
x(eliminate) = []
y(eliminate) = []
S(x,y)
creates a matrix of where x
and y
are considered to be permutable, i.e. it will be 4x4 in this example. We only need the pairs you present, which will be on the diagonal of this matrix; use diag()
to obtain those. Then, do the logical check with <0.1
and finally use the same removal trick as above.
Related Topics
How to Extract Data from Matplotlib Plot
Python Dictionary:Typeerror: Unhashable Type: 'List'
How to Extract Top-Level Domain Name (Tld) from Url
How to Make Python Scripts Executable on Windows
Search for "Does-Not-Contain" on a Dataframe in Pandas
How to Get a Thread Safe Print in Python 2.6
Call Int() Function on Every List Element
Multithreaded Blas in Python/Numpy
Modules Are Installed Using Pip on Osx But Not Found When Importing
Functions That Help to Understand JSON(Dict) Structure
How to Find the Current Os in Python
How to Add Items into a Numpy Array
How to Reverse Lists in Python, Getting "Nonetype" as List
Pandas: Cast Column to String Does Not Work
How to Read Hdf5 Files in Python