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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:33:51+00:00 2026-06-15T02:33:51+00:00

I have a numpy array of zeros. For concreteness, suppose it’s 2x3x4: x =

  • 0

I have a numpy array of zeros. For concreteness, suppose it’s 2x3x4:

x = np.zeros((2,3,4))

and suppose I have a 2×3 array of random integers from 0 to 3 (the index of the 3rd dimension of x).

>>> y = sp.stats.distributions.randint.rvs(0, 4, size=(2,3))
>>> y
[[2 1 0]
 [3 2 0]]

How do I do the following assignments efficiently (edit: something that doesn’t use for loops and works for x with any number of dimensions and any number of elements in each dimension)?

>>> x[0,0,y[0,0]]=1
>>> x[0,1,y[0,1]]=1
>>> x[0,2,y[0,2]]=1
>>> x[1,0,y[1,0]]=1
>>> x[1,1,y[1,1]]=1
>>> x[1,2,y[1,2]]=1
>>> x
array([[[ 0.,  0.,  1.,  0.],
        [ 0.,  1.,  0.,  0.],
        [ 1.,  0.,  0.,  0.]],

       [[ 0.,  0.,  0.,  1.],
        [ 0.,  0.,  1.,  0.],
        [ 1.,  0.,  0.,  0.]]])

Thanks,
James

  • 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-15T02:33:53+00:00Added an answer on June 15, 2026 at 2:33 am

    At the moment, I can only think of the “simple” version, which involves flattening along the first two dimensions. This code should work:

    shape_last = x.shape[-1]
    x.reshape((-1, shape_last))[np.arange(y.size), y.flatten()] = 1
    

    This yields (with my randomly-generated y):

    array([[[ 0.,  0.,  0.,  1.],
            [ 0.,  0.,  1.,  0.],
            [ 0.,  1.,  0.,  0.]],
    
           [[ 0.,  1.,  0.,  0.],
            [ 0.,  0.,  0.,  1.],
            [ 0.,  1.,  0.,  0.]]])
    

    The key is, if you do an indexing using multiple numpy arrays (advanced indexing), numpy will use pairs of indices to index into the array.

    Of course, make sure x and y are both either C-order or F-order — otherwise, the calls to reshape and flatten might give different orders.

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

Sidebar

Related Questions

Let's say I have an array of zeros: a = numpy.zeros(1000) I then introduce
I am creating an object in python. I have a numpy array from an
I have created an array thusly: import numpy as np data = np.zeros( (512,512,3),
I have an numpy array which I save to a image using savefig(). Then
I have a numpy array like this: x = np.array([[1,2,3],[4,5,6],[7,8,9]]) I need to create
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 numpy array like this a = np.array(1) Now if I want
i have a numpy array like the following [[ 1 2 3 4 ]
A toy-case for my problem: I have a numpy array of size, say, 1000:

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.