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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:47:52+00:00 2026-06-09T19:47:52+00:00

Given an index and a size, is there a more efficient way to produce

  • 0

Given an index and a size, is there a more efficient way to produce the standard basis vector:

import numpy as np
np.array([1.0 if i == index else 0.0 for i in range(size)])
  • 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-09T19:47:53+00:00Added an answer on June 9, 2026 at 7:47 pm
    x = np.zeros(size)
    x[index] = 1.0
    

    at least i think thats it…

    >>> t = timeit.Timer('np.array([1.0 if i == index else 0.0 for i in range(size)]
    )','import numpy as np;size=10000;index=5123')
    >>> t.timeit(10)
    0.039461429317952934  #original method
    >>> t = timeit.Timer('x=np.zeros(size);x[index]=1.0','import numpy as np;size=10000;index=5123')
    >>> t.timeit(10)
    9.4077963240124518e-05 #zeros method
    >>> t = timeit.Timer('x=np.eye(1.0,size,index)','import numpy as np;size=10000;index=5123')
    >>> t.timeit(10)
    0.0001398340635319073 #eye method
    

    looks like np.zeros is fastest…

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

Sidebar

Related Questions

Is there a good way to start at a given index and move BACKWARDS
QUESTION: Given a cell index (red) compute the array index (black) that surround the
Given an array. How can we find sum of elements in index interval (i,
I'm wondering if the index of an array can be given a name in
Let's say I have an numpy array A of size n x m x
Given an array of size n I want to generate random probabilities for each
How do I set an item as the selected item programmatically, given its index?
Given the following model, I want to index the fields (sequence,stock) class QuoteModel(models.Model): quotedate
Given an R list, I wish to find the index of a given list
I have an integer array of size 50. So, as you know, initially the

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.