[Numpy-discussion] Why does argwhere return column vector?

mark markbak@gmail....
Tue May 27 03:44:34 CDT 2008


OK, I get how it works for 2D arrays.

What I want to do is insert a number, say 7, before every value in the
array that is larger than, for example, 1.

Then I need to first find all the indices of values larger than 1, and
then I can do an insert:

>>> a = arange(5)
>>> i = argwhere( a>1 )
>>> insert(a,i[:,0],7)
array([0, 1, 7, 2, 7, 3, 7, 4])

Is there a better way to do this?
So for this instance, it is inconvenient that argwhere returns a
column vector. But I understand the issue for arrays with higher
dimensions.

Thanks for the explanation,

Mark

> Each row of the result is a coordinate into your array.  So if you had
>
> a = np.arange(12).reshape((4,3))
>
> then you'd get
>
> In [54]: np.argwhere(a>5)
> Out[54]:
> array([[2, 0],
>        [2, 1],
>        [2, 2],
>        [3, 0],
>        [3, 1],
>        [3, 2]])
>
> If you want to grab the elements larger than 5, just do
>
> a[a>5]


More information about the Numpy-discussion mailing list