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

  • Home
  • SEARCH
  • 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 6922027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:21:11+00:00 2026-05-27T10:21:11+00:00

I have a 2D numpy array (i.e matrix) A which contains useful data interspread

  • 0

I have a 2D numpy array (i.e matrix) A which contains useful data interspread with garbage in the form of column vectors as well as a ‘selection’ array B which contains ‘1’ for those columns that are important and 0 for those that are not. Is there a way to select only those columns from A that correspond to ones in B? i.e i have a matrix

A = array([[ 0,  1,  2,  3,  4],   and a vector B = array([ 0,  1,  0,  1,  0])
           [ 5,  6,  7,  8,  9],
           [10, 11, 12, 13, 14],
           [15, 16, 17, 18, 19],
           [20, 21, 22, 23, 24]])

and I want

array([[1,   3],
       [6,   8],
       [11, 13],
       [16, 18],
       [21, 23]])

Is there an elegant way to do so? Right now i just have a for loop that iterates through B.

NOTE: the matrices that i’m dealing with are large, so i don’t want to use numpy masked arrays, as i simply don’t want the masked data

  • 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-05-27T10:21:11+00:00Added an answer on May 27, 2026 at 10:21 am
    >>> A
      array([[ 0,  1,  2,  3,  4],
             [ 5,  6,  7,  8,  9],
             [10, 11, 12, 13, 14],
             [15, 16, 17, 18, 19],
             [20, 21, 22, 23, 24]])
    >>> B = NP.array([ 0,  1,  0,  1,  0])
    
    >>> # convert the indexing array to a boolean array
    >>> B = NP.array(B, dtype=bool)
    
    >>> # index A against B--indexing array is placed after the ',' because
    >>> # you are selecting columns
    
    >>> res = A[:,B]
    
    >>> res
      array([[ 1,  3],
             [ 6,  8],
             [11, 13],
             [16, 18],
             [21, 23]])  
    


    The syntax for index-based slicing in NumPy is elegant and simple. A couple of rules cover a majority of use cases:

    • the form is [rows, columns]

    • specify all rows or all columns using a colon “:” e.g., [:, 4] (extracts the
      entire 5th column)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two identically-sized numpy.array objects (both one-dimensional), one of which contains a list
I have a matrix in the type of a Numpy array. How would I
I am using numpy. I have a matrix with 1 column and N rows
I have a numpy array of pixel data that I want to draw at
I have a numpy.array of 640x480 images, each of which is 630 images long.
I have a NumPy array [1,2,3,4,5,6,7,8,9,10,11,12,13,14] and want to have an array structured like
I have a numpy array with positive and negative values in. a = array([1,1,-1,-2,-3,4,5])
I have a problem with some numpy stuff. I need a numpy array to
Let's say I have an array of zeros: a = numpy.zeros(1000) I then introduce
I have a Numpy array that looks like >>> a array([[ 3. , 2.

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.