Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save smidm/6cb14ac3d82fd24c657d to your computer and use it in GitHub Desktop.
Save smidm/6cb14ac3d82fd24c657d to your computer and use it in GitHub Desktop.
# modified numpy concatenate, vstack, hstack accepting scalars in the input tuple
def concatenate(tup, axis=0):
tup_arrays = [x for x in tup if isinstance(x, np.ndarray)]
shape = list(tup_arrays[0].shape)
shape[axis] = 1
tup_ = []
for x in tup:
if isinstance(x, (int, long, float)):
tup_.append(np.ones(shape) * x)
else:
tup_.append(x)
return np.concatenate(tup_, axis=axis)
def vstack(tup):
return concatenate(tup, 0)
def hstack(tup):
return concatenate(tup, 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment