1

Suppose you have an array (m, m) and want to make it (n, n). For example, transforming a 2x2 matrix to a 6x6. So:

[[ 1.  2.]
 [ 3.  4.]]

To:

[[ 1.  2.  0.  0.  0.  0.]
 [ 3.  4.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

This is what I'm doing:

def array_append(old_array, new_shape):
    old_shape = old_array.shape
    dif = np.array(new_shape) - np.array(old_array.shape)
    rows = []
    for i in xrange(dif[0]):
        rows.append(np.zeros((old_array.shape[0])).tolist())
    new_array = np.append(old_array, rows, axis=0)
    columns = []
    for i in xrange(len(new_array)):
        columns.append(np.zeros(dif[1]).tolist())
    return np.append(new_array, columns, axis=1)

Example use:

test1 = np.ones((2,2))
test2 = np.zeros((6,6))
print array_append(test1, test2.shape)

Output:

[[ 1.  1.  0.  0.  0.  0.]
 [ 1.  1.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.  0.]]

Based on this answer. But that's a lot of code for an (imho) simple operation. Is there a more concise/pythonic way to do it?

1
  • @pnodnda: your approach is way too complicated. Simply allocate new array and copy old to appropriate location. It's just that simple, as demonstrated on Benjamins (modified) and my answer. Btw the word append is commonly associated with dynamic data structures, which numpy.array is not. Thanks Commented Feb 1, 2011 at 21:27

2 Answers 2

3

Why not use array = numpy.zeros((6,6)), see the numpy docs...

EDIT, woops, question has been edited... I guess you are trying to put ones in a section of an array filled with zeros? Then:

array = numpy.zeros((6,6))
array[0:2,0:2] = 1

If the small matrix does not all have the value of 1:

array[ystart:yend,xstart:xend] = smallermatrix
Sign up to request clarification or add additional context in comments.

5 Comments

I only added input/output to the code I wrote. You should read the code before replying. And I'm not trying to put ones in an array filled with zeros. I'm taking a small "matrix" and increasing its dimension by padding/filling it with zeros.
In that case, create a blank matrix of the correct size with numpy.zeros and insert your smaller matrix as I show above.
@pnodbnda You should maybe entertain the possibility that @Benjamin answered as he did, not to spite you or to be lazy, but because your question is lacking in clarity. The headline is about the only thing that is clear about it, and obviously it doesn't even reflect that well on the content of the question. StackOverflow is a community built on manners and professionalism, please do try to practice that in future remarks on people's efforts.
@Benjamin Your code assumes the whole matrix has the same value. It doesn't.
@Banang Yes, I apologize. But hurrying to answer a question without reading it and then trying to pretend you've actually answered anything when you haven't isn't exactly the right attitude either. I'll see if I can edit the OP to make it clearer.
1

That would be then:

# test1= np.ones((2, 2))
test1= np.random.randn((2, 2))
test2= np.zeros((6, 6))
test2[0: 2, 0: 2]= test1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.