def resize(vec, size):
"""
Resizes a vector such that it has the right size. This is done by repeating the vector
in each dimension until the required size is reached. Note an error is thrown if 'size'
is not a multiple of the size of vec.
vec A 1D or 2D numpy array
size A list of dimension sizes (e.g. [2,3])
"""
if not isinstance(vec, (np.ndarray)):
vec_resized = vec * np.ones(size)
elif vec.shape[0] == size[0] and len(vec.shape) == 1:
vec_resized = np.reshape(np.repeat(vec, size[1]), size)
elif vec.shape[0] == 1 and len(vec.shape) == 1:
vec_resized = vec*np.ones(size)
else:
# Check that the output dims are multiples of input dims
assert(size[0] % vec.shape[0] == 0)
assert(size[1] % vec.shape[1] == 0)
vec_resized = np.tile(vec, (size[0] // vec.shape[0], size[1] // vec.shape[1]))
return vec_resized
评论列表
文章目录