problems with duplicating and slicing an array

Hi everyone, I have two questions: 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. 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? Thanks!!! -Y

On Thu, 20 Jan 2005 20:29:26 -0500 Yun Mao <yunmao@gmail.com> wrote:
Hi everyone, I have two questions: 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.
v = na.array(u)
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?
have a look at the "take" method. Simon.

Simon Burton wrote:
On Thu, 20 Jan 2005 20:29:26 -0500 Yun Mao <yunmao@gmail.com> wrote:
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]
have a look at the "take" method.
or use numarray:
import numarray as N i = N.array([0, 2]) x = N.array([1.1, 2.2, 3.3, 4.4]) y = x[i] y array([ 1.1, 3.3])
-- Christopher Barker, Ph.D. Oceanographer NOAA/OR&R/HAZMAT (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

Chris Barker wrote:
Simon Burton wrote:
On Thu, 20 Jan 2005 20:29:26 -0500 Yun Mao <yunmao@gmail.com> wrote:
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]
have a look at the "take" method.
or use numarray:
import numarray as N i = N.array([0, 2]) x = N.array([1.1, 2.2, 3.3, 4.4]) y = x[i] y array([ 1.1, 3.3])
Or use scipy: from scipy import * alter_numeric() i = array([0,2]) x = array([1.1,2.2,3.3,4.4]) y = x[i] print y [1.1 3.3]

On Fri, 21 Jan 2005, Travis Oliphant apparently wrote:
from scipy import * alter_numeric() i = array([0,2]) x = array([1.1,2.2,3.3,4.4]) y = x[i]
This ^ gives me an invalid index error. scipy version 0.3.0_266.4242 Alan Isaac

Alan G Isaac wrote:
On Fri, 21 Jan 2005, Travis Oliphant apparently wrote:
from scipy import * alter_numeric() i = array([0,2]) x = array([1.1,2.2,3.3,4.4]) y = x[i]
This ^ gives me an invalid index error. scipy version 0.3.0_266.4242
Travis's example works for me at scipy 0.3.2_302.4549 (from CVS), Numeric 23.6, numarray 1.1.1, all on FC3.

Alan G Isaac wrote:
On Fri, 21 Jan 2005, Travis Oliphant apparently wrote:
from scipy import * alter_numeric() i = array([0,2]) x = array([1.1,2.2,3.3,4.4]) y = x[i]
This ^ gives me an invalid index error. scipy version 0.3.0_266.4242
Your version of scipy is apparently too low. Mine is 0.3.2_299.4506 -Travis

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@llb.saclay.cea.fr ------------------------------------------------------------------------ -------

On Jan 21, 2005, at 9:48, konrad.hinsen@laposte.net wrote:
There are several ways to make a copy of an array. My personal preference is
import copy v = copy(u)
That's of course import copy v = copy.copy(u) or from copy import copy v = copy(u) Konrad. -- --------------------------------------------------------------------- Konrad Hinsen Laboratoire Léon 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@llb.saclay.cea.fr ---------------------------------------------------------------------
participants (7)
-
Alan G Isaac
-
Chris Barker
-
konrad.hinsen@laposte.net
-
Simon Burton
-
Stephen Walton
-
Travis Oliphant
-
Yun Mao