finding nonzero elements in list
hi all, i'm trying to find nonzero elements in an array, as follows: a = array([[1, 0], [1, 1], [1, 1], [0, 1]]) i want to find all elements that are [1,1]. i tried: nonzero(a == [1,0]) but i cannot interpret the output. the output i get is: (array([0, 0, 1, 2]), array([0, 1, 0, 0])) i simply want to find the indices of the elements that equal [1,0]. how can i do this? thanks.
On Mon, Oct 12, 2009 at 10:18 AM, per freem <perfreem@gmail.com> wrote:
hi all,
i'm trying to find nonzero elements in an array, as follows:
a = array([[1, 0], [1, 1], [1, 1], [0, 1]])
i want to find all elements that are [1,1]. i tried: nonzero(a == [1,0]) but i cannot interpret the output. the output i get is: (array([0, 0, 1, 2]), array([0, 1, 0, 0]))
i simply want to find the indices of the elements that equal [1,0]. how can i do this? thanks. _______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
a == [1,0] does elementwise comparison, you need to aggregate condition for all elements of row
a = np.array([[1, 0], [1, 1], [1, 1], [0, 1]]) np.nonzero((a==[1,0]).all(1)) (array([0]),) np.where((a==[1,0]).all(1)) (array([0]),) np.nonzero((a==[1,1]).all(1)) (array([1, 2]),)
Josef
On Mon, Oct 12, 2009 at 9:18 AM, per freem <perfreem@gmail.com> wrote:
hi all,
i'm trying to find nonzero elements in an array, as follows:
a = array([[1, 0], [1, 1], [1, 1], [0, 1]])
i want to find all elements that are [1,1]. i tried: nonzero(a == [1,0]) but i cannot interpret the output. the output i get is: (array([0, 0, 1, 2]), array([0, 1, 0, 0]))
i simply want to find the indices of the elements that equal [1,0]. how can i do this? thanks.
You might simply apply a mask to your array satisfying the condition: I[1]: a = array([[1, 0], ...: [1, 1], ...: [1, 1], ...: [0, 1]]) I[2]: a == [1,0] O[2]: array([[ True, True], [ True, False], [ True, False], [False, False]], dtype=bool) I[3]: a[a==[1,0]] O[3]: array([1, 0, 1, 1])
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Gökhan
On Mon, Oct 12, 2009 at 9:39 AM, Gökhan Sever <gokhansever@gmail.com> wrote:
On Mon, Oct 12, 2009 at 9:18 AM, per freem <perfreem@gmail.com> wrote:
hi all,
i'm trying to find nonzero elements in an array, as follows:
a = array([[1, 0], [1, 1], [1, 1], [0, 1]])
i want to find all elements that are [1,1]. i tried: nonzero(a == [1,0]) but i cannot interpret the output. the output i get is: (array([0, 0, 1, 2]), array([0, 1, 0, 0]))
i simply want to find the indices of the elements that equal [1,0]. how can i do this? thanks.
You might simply apply a mask to your array satisfying the condition:
I[1]: a = array([[1, 0], ...: [1, 1], ...: [1, 1], ...: [0, 1]])
I[2]: a == [1,0] O[2]: array([[ True, True], [ True, False], [ True, False], [False, False]], dtype=bool)
I[3]: a[a==[1,0]] O[3]: array([1, 0, 1, 1])
Addendum; This might work better since you are looking non-zero elements I[19]: a[a==[1,0]] & a[a==[0,1]] O[19]: array([1, 0, 0, 1])
_______________________________________________ NumPy-Discussion mailing list NumPy-Discussion@scipy.org http://mail.scipy.org/mailman/listinfo/numpy-discussion
-- Gökhan
-- Gökhan
participants (3)
-
Gökhan Sever
-
josef.pktd@gmail.com
-
per freem