Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8806533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T02:09:00+00:00 2026-06-14T02:09:00+00:00

Given a numpy matrix A of shape (m,n) and a list ind of Boolean

  • 0

Given a numpy matrix A of shape (m,n) and a list ind of Boolean values with length n, I want to extract the columns of A where the corresponding element of the Boolean list is true.

My first naive try

Asub = A[:,cols]

gives quite weird results, not necessary to cite here…

Following pv.’s answer to this question, I tried with numpy.ix_ as follows:

>>> A = numpy.diag([1,2,3])
>>> ind = [True, True, False]
>>> A[:,numpy.ix_(ind)]
array([[[1, 0]],

       [[0, 2]],

       [[0, 0]]])
>>> A[:,numpy.ix_(ind)].shape
(3, 1, 2)

Now the result is of inappropriate shape: I want to have a (3,2) resulting array. I guess I could collapse the result down to two dimensions, but there surely must be a better way of doing this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T02:09:01+00:00Added an answer on June 14, 2026 at 2:09 am

    As the docs discuss, boolean indexing like you want “occurs when obj is an array object of Boolean type (such as may be returned from comparison operators).”

    IOW, ind needs to be an ndarray of type bool:

    In [15]: A = numpy.diag([1,2,3])
    
    In [16]: ind = [True, True, False]
    
    In [17]: A[:,ind]
    Out[17]: 
    array([[0, 0, 1],
           [2, 2, 0],
           [0, 0, 0]])
    

    which occurs because it’s interpreting the bools as integers, and giving you columns [1, 1, 0].

    OTOH:

    In [18]: A[:,numpy.array(ind)]
    Out[18]: 
    array([[1, 0],
           [0, 2],
           [0, 0]])
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list, e.g. x = [True]*20 , I want to assign False to
given a list of module names (e.g. mymods = ['numpy', 'scipy', ...]) how can
Given an NxM feature vectors as numpy matrix. Is there any routine that can
I want to implement my own matrix-class that inherits from numpy's matrix class. numpy's
Given the following Markov Matrix: import numpy, scipy.linalg A = numpy.array([[0.9, 0.1],[0.15, 0.85]]) The
In Numpy, ix_() is used to grab rows and columns of a matrix, but
Given 2 2x2 Numpy arrays, each element having a value between 0 and 1,
I want to replace the max values of each column of a 2d numpy
I want to test an unknown value against the constraints that a given NumPy
Given a certain date, I want to set the value of a cell with

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.