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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:09:46+00:00 2026-06-13T03:09:46+00:00

I have a list pts containing N points (Python floats). I wish to construct

  • 0

I have a list pts containing N points (Python floats). I wish to construct a NumPy array of dimension N*N*N*3 such that the array is equivalent to:

for i in xrange(0, N):
    for j in xrange(0, N):
        for k in xrange(0, N):
            arr[i,j,k,0] = pts[i]
            arr[i,j,k,1] = pts[j]
            arr[i,j,k,2] = pts[k]

I am wondering how I can exploit the array broadcasting rules of NumPy and functions such as tile to simplify 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-13T03:09:47+00:00Added an answer on June 13, 2026 at 3:09 am

    I think that the following should work:

    pts = np.array(pts)  #Skip if pts is a numpy array already
    lp = len(pts)
    arr = np.zeros((lp,lp,lp,3))
    arr[:,:,:,0] = pts[:,None,None]  #None is the same as np.newaxis
    arr[:,:,:,1] = pts[None,:,None]
    arr[:,:,:,2] = pts[None,None,:]
    

    A quick test:

    import numpy as np
    import timeit
    
    def meth1(pts):
       pts = np.array(pts)  #Skip if pts is a numpy array already
       lp = len(pts)
       arr = np.zeros((lp,lp,lp,3))
       arr[:,:,:,0] = pts[:,None,None]  #None is the same as np.newaxis
       arr[:,:,:,1] = pts[None,:,None]
       arr[:,:,:,2] = pts[None,None,:]
       return arr
    
    def meth2(pts):
       lp = len(pts)
       N = lp
       arr = np.zeros((lp,lp,lp,3))
       for i in xrange(0, N):
          for j in xrange(0, N):
             for k in xrange(0, N):
                arr[i,j,k,0] = pts[i]
                arr[i,j,k,1] = pts[j]
                arr[i,j,k,2] = pts[k]
    
       return arr
    
    pts = range(10)
    a1 = meth1(pts)
    a2 = meth2(pts)
    
    print np.all(a1 == a2)
    
    NREPEAT = 10000
    print timeit.timeit('meth1(pts)','from __main__ import meth1,pts',number=NREPEAT)
    print timeit.timeit('meth2(pts)','from __main__ import meth2,pts',number=NREPEAT)
    

    results in:

    True
    0.873255968094   #my way
    11.4249279499    #original
    

    So this new method is an order of magnitude faster as well.

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

Sidebar

Related Questions

i have list of rows that user select and i want to delete them,
I have List with n items. I wish transform my list to new list,
I have list of dictonaries. I wish type iterate over this list and map
I have list of items that need to be processed and printed to the
I have list of rows that I have fetched using mysql_fetch_array() . Now I
I have list of comments in one array. Can I use update_all on array
I have created a custom ListActivity that shows a list of skills and buttons
I have List collection that is populated in specific order (the requirement is that,
i have list like that: <ul> <li><a...>...</a></li> <li>...</li> </ul> where both type of listelements
I have list of text that contain double quoate and upderscore. How can I

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.