2

I need to create an array of a specific size mxn filled with empty values so that when I concatenate to that array the initial values will be overwritten with the added values.

My current code:

a = numpy.empty([2,2])  # Make empty 2x2 matrix
b = numpy.array([[1,2],[3,4]])    # Make example 2x2 matrix
myArray = numpy.concatenate((a,b))    # Combine empty and example arrays

Unfortunately, I end up making a 4x2 matrix instead of a 2x2 matrix with the values of b.

Is there anyway to make an actually empty array of a certain size so when I concatenate to it, the values of it become my added values instead of the default + added values?

1
  • Why not just do a[:]=b? Or a=b.copy()? Do you want to keep any initial elements of a after this operation? Commented Feb 2, 2016 at 7:50

3 Answers 3

2

Like Oniow said, concatenate does exactly what you saw.

If you want 'default values' that will differ from regular scalar elements, I would suggest you to initialize your array with NaNs (as your 'default value'). If I understand your question, you want to merge matrices so that regular scalars will override your 'default value' elements.

Anyway I suggest you to add the following:

def get_default(size_x,size_y):
    # returns a new matrix filled with 'default values'
    tmp = np.empty([size_x,size_y])
    tmp.fill(np.nan)
    return tmp

And also:

def merge(a, b):
    l = lambda x, y: y if np.isnan(x) else x
    l = np.vectorize(l)
    return map(l, a, b)

Note that if you merge 2 matrices, and both values are non 'default' then it will take the value of the left matrix.

Using NaNs as default value, will result the expected behavior from a default value, for example all math ops will result 'default' as this value indicates that you don't really care about this index in the matrix.

Sign up to request clarification or add additional context in comments.

1 Comment

Be ware of 2 things - np.nan is a float, so this is a good thing to do with dtype=int arrays. And your vectorize operation is almost as slow as plain iteration over all elements (at most a 2x speedup). It will be much slower than tmp[:]=b, especially for large arrays.
1

If I understand your question correctly - concatenate is not what you are looking for. Concatenate does as you saw: joins along an axis.

If you are trying to have an empty matrix that becomes the values of another you could do the following:

import numpy as np

a = np.zeros([2,2])
b = np.array([[1,2],[3,4]])

my_array = a + b

--or--

import numpy as np

my_array = np.zeros([2,2]) # you can use empty here instead in this case.

my_array[0,0] = float(input('Enter first value: ')) # However you get your data to put them into the arrays.

But, I am guessing that is not what you really want as you could just use my_array = b. If you edit your question with more info I may be able to help more.

If you are worried about values adding over time to your array...

import numpy as np

a = np.zeros([2,2])

my_array = b # b is some other 2x2 matrix
''' Do stuff '''
new_b # New array has appeared
my_array = new_b # update your array to these new values. values will not add. 

# Note: if you make changes to my_array those changes will carry onto new_b. To avoid this at the cost of some memory:
my_array = np.copy(new_b)

Comments

0

I'll add a similar case, if someone wants to do something in the same manner than:

growing_list = []
for value in values_to_add:
    growing_list.append(value)

But with a numpy empty array, I've sometimes seen a pythonic way to do it like this:

for value in values_to_add:
    try:
        growing_ndarray.concatenate((growing_ndarray, value), axis=0)
    except UnboundLocalError as e:
        growing_ndarray = np.asarray(value)

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.