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 8859147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:59:06+00:00 2026-06-14T14:59:06+00:00

How do i carve or mask a 2D numpy array according to an index

  • 0

How do i “carve” or mask a 2D numpy array according to an index formula? I don’t care what the element value is, only its position in the array.

For example, given an mxm array, how do I extract all elements whose address conforms to

for i in range(0,m):
    for j in range(0,m):
        if j-i-k>=0:
            A[i,j] = 1
        elif j-p-k>=0:
            A[i,j] = 1
        elif i-k>=0:
            A[i,j] = 1
        else:
            A[i,j] = 0
        j=j+1
    i=i+1

where

k and p are an arbitrary fences

Assume

k<m
p<m

This ends up looking like a diagonal slice + a horizontal slice + a vertical slice. Can it be done without the for loops above?

  • 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-14T14:59:07+00:00Added an answer on June 14, 2026 at 2:59 pm
    xdim,ydim = data.shape
    k = 2
    a, b = np.meshgrid(range(ydim),range(xdim))
    mask = (b - a -k) < 0
    
    new_data = data[mask]
    
    new_data2 = np.array(data) # to force a copy
    new_data2[~mask] = 0
    

    new_data is a vector because the masking processes (done this way) flattens the array. Your are selecting a shape with a ragged that cannot be represented as an array. If you just want to set the non-selcted values to 0, use new_data2.

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

Sidebar

Related Questions

I don't care if its something Apple wont accept but, how would I get
Usually I don't care about the value of the item in the set, I
I don't care whether it is a string, stringlist, memo, etc ... but not
I don't really care about encoding and stuff, as long as I get back
If I don't care whether Thread1 changes Flag1 at the same time Thread2 changes
I don't really care which line in a class was hit. I just want
I don't care about which key was pressed, or how long, or anything like
I don't care what the differences are. I just want to know whether the
Actually I don't care too much about Mac OS X development. I want to
(I don't really care if it's my fault but rather why things are happening,

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.