[Numpy-discussion] problems with duplicating and slicing an array

konrad.hinsen at laposte.net konrad.hinsen at laposte.net
Fri Jan 21 00:50:14 CST 2005


On 21.01.2005, at 02:29, Yun Mao wrote:

> 1. When I do v = u[:, :],  it seems u and v still point to the same
> memory. e.g. When I do v[1,1]=0, u[1,1] will be zero out as well.
> What's the right way to duplicate an array? Now I have to do v =
> dot(u, identity(N)), which is kind of silly.

There are several ways to make a copy of an array. My personal  
preference is

	import copy
	v = copy(u)

because this is a general mechanism that works for all Python objects.

> 2. Is there a way to do Matlab style slicing? e.g. if I have
>   i = array([0, 2])
>   x = array([1.1, 2.2, 3.3, 4.4])
>   I wish y = x(i) would give me [1.1, 3.3]
> Now I'm using map, but it gets a little annoying when there are two
> dimensions. Any ideas?

y = Numeric.take(x, i)

Konrad.
--
------------------------------------------------------------------------ 
-------
Konrad Hinsen
Laboratoire Leon Brillouin, CEA Saclay,
91191 Gif-sur-Yvette Cedex, France
Tel.: +33-1 69 08 79 25
Fax: +33-1 69 08 82 61
E-Mail: hinsen at llb.saclay.cea.fr
------------------------------------------------------------------------ 
-------





More information about the Numpy-discussion mailing list