[SciPy-user] [newbie] array cloning with new size

Christian Kristukat ckkart at hoc.net
Mon Oct 24 07:00:07 CDT 2005

Mauro Cherubini wrote:
> Dear All,
> sorry for the silly question, but I am struggling with the following  
> problem:
> I have to clone a 2D array into another 2D array with an extra empty  
> line. Then I want to copy the first line of the array into the empty  
> last line of the array.

This should happen automatically when you resize the array along the rows (first
dimension). The new lines are filled with the values from the beginning of the

> I tried a couple of things to get it sorted but none of them worked.  
> The last attempt is reported below (my matrix has three columns),  
> where I used the resize() function, which throws back this error from  
> scipy:
> ...
>    File "/Users/mauro/Documents/workspace/tag-wai/ 
> TagsNetworkMaker.py", line 52, in matrix_preparator
>      new_matrix = resize(datamatrix, [lato +1, 3])
>    File "/System/Library/Frameworks/Python.framework/Versions/2.3/lib/ 
> python2.3/site-packages/scipy/base/oldnumeric.py", line 196, in resize
>      total_size = up.multiply.reduce(new_shape)
> NameError: global name 'up' is not defined

I do not get that error. Does resize in general work as expected? Probably it's
a problem with your numeric/scipy installation.

> By the way, can anyone explain me how to slice and assign a submatrix  
> from the original matrix? I tried this syntax with no luck:
> new_matrix[: lato, :] = datamatrix[:, :]

This is correct and should work. Check if both sides really have the same shape.

> ----------------------------
>      # the following method should prepare the matrix for the  
> processing. In fact, we need to have a poligon set of points
>      # which is by definition closed so the first point should be  
> added into the matrix as last point
>      def matrix_preparator(self, datamatrix):
>          # first we find the number of points in the matrix
>          lato = datamatrix.shape[0]
>          # if the matrix contains only one row then it exit
>          if lato > 1:
>              # now we create a new matrix with an extra line
>              new_matrix = resize(datamatrix, [lato +1, 3])
>              print new_matrix
>              # here we clone the first point to the last line
>              new_matrix[lato + 1, :] = datamatrix[0, :]

'lato+1' is one too much. The last line's index is just 'lato'.

>              # now the matrix is ready for processing
>              return new_matrix;
>          else:
>              return datamatrix;


More information about the SciPy-user mailing list