Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

atleast_2d axis argument option #12336

Copy link
Copy link
Open
@scottstanie

Description

@scottstanie
Issue body actions

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)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.