Open
Description
Would it be possible to add some option to the atleast_2d
function so that you can specify where you'd like the newaxis
to be placed? I'd like to be able to add the newaxis
as the last dimension to force the array to a column vector.
My use case is that I want to have a column vector, but the input may appear as (N,k), or an (N,) array. If I use atleast_2d
, then I would need to transpose the result if the original shape was (N,), but shouldn't transpose if the shape was already a column.
Reproducing code example:
import numpy as np
>>> A = np.empty((5, 3))
>>> rhs1 = np.arange(10).reshape((5, 2))
>>> rhs2 = np.arange(5)
>>> A[:, (0, 1)] = rhs1
>>> A[:, [2]] = rhs2
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: value array of shape (5,) could not be broadcast to indexing result of shape (1,5)
# Other option:
>>> A[:, [2]] = np.atleast_2d(rhs2).T
>>> A[:, [0, 1]] = np.atleast_2d(rhs1).T
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: shape mismatch: value array of shape (1,5) could not be broadcast to indexing result of shape (1,5)
# Ideally what I want:
>>> A[:, [0, 1]] = np.atleast_2d(rhs1, axis=-1)
>>> A[:, [2]] = np.atleast_2d(rhs2, axis=1)