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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:44:09+00:00 2026-05-15T19:44:09+00:00

I’m looking for a fast way to interconvert between linear and multidimensional indexing in

  • 0

I’m looking for a fast way to interconvert between linear and multidimensional indexing in Numpy.

To make my usage concrete, I have a large collection of N particles, each assigned 5 float values (dimensions) giving an Nx5 array. I then bin each dimension using numpy.digitize with an appropriate choice of bin boundaries to assign each particle a bin in the 5 dimensional space.

N = 10
ndims = 5
p = numpy.random.normal(size=(N,ndims))
for idim in xrange(ndims):
    bbnds[idim] = numpy.array([-float('inf')]+[-2.,-1.,0.,1.,2.]+[float('inf')])

binassign = ndims*[None]
for idim in xrange(ndims):
    binassign[idim] = numpy.digitize(p[:,idim],bbnds[idim]) - 1

binassign then contains rows that correspond to the multidimensional index. If I then want to convert the multidimensional index to a linear index, I think I would want to do something like:

linind = numpy.arange(6**5).reshape(6,6,6,6,6)

This would give a look-up for each multidimensional index to map it to a linear index. You could then go back using:

mindx = numpy.unravel_index(x,linind.shape)

Where I’m having difficulties is figuring out how to take binassign (the Nx5 array) containing the multidimensional index in each row, and coverting that to an 1d linear index, by using it to slice the linear indexing array linind.

If anyone has a one (or several) line indexing trick to go back and forth between the multidimensional index and the linear index in a way that vectorizes the operation for all N particles, I would appreciate your insight.

  • 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-15T19:44:10+00:00Added an answer on May 15, 2026 at 7:44 pm

    Although I very much like EOL’s answer, I wanted to generalize it a bit for non-uniform numbers of bins along each direction, and also to highlight the differences between C and F styles of ordering. Here is an example solution:

    ndims = 5
    N = 10
    
    # Define bin boundaries 
    binbnds = ndims*[None]
    nbins = []
    for idim in xrange(ndims):
        binbnds[idim] = numpy.linspace(-10.0,10.0,numpy.random.randint(2,15))
        binbnds[idim][0] = -float('inf')
        binbnds[idim][-1] = float('inf')
        nbins.append(binbnds[idim].shape[0]-1)
    
    nstates = numpy.cumprod(nbins)[-1]
    
    # Define variable values for N particles in ndims dimensions
    p = numpy.random.normal(size=(N,ndims))
    
    # Assign to bins along each dimension
    binassign = ndims*[None]
    for idim in xrange(ndims):
        binassign[idim] = numpy.digitize(p[:,idim],binbnds[idim]) - 1
    
    binassign = numpy.array(binassign)
    
    # multidimensional array with elements mapping from multidim to linear index
    # Two different arrays for C vs F ordering
    linind_C = numpy.arange(nstates).reshape(nbins,order='C')
    linind_F = numpy.arange(nstates).reshape(nbins,order='F')
    

    and now make the conversion

    # Fast conversion to linear index
    b_F = numpy.cumprod([1] + nbins)[:-1]
    b_C = numpy.cumprod([1] + nbins[::-1])[:-1][::-1]
    
    box_index_F = numpy.dot(b_F,binassign)
    box_index_C = numpy.dot(b_C,binassign)
    

    and to check for correctness:

    # Check
    print 'Checking correct mapping for each particle F order'
    for k in xrange(N):
        ii = box_index_F[k]
        jj = linind_F[tuple(binassign[:,k])]
        print 'particle %d %s (%d %d)' % (k,ii == jj,ii,jj)
    
    print 'Checking correct mapping for each particle C order'
    for k in xrange(N):
        ii = box_index_C[k]
        jj = linind_C[tuple(binassign[:,k])]
        print 'particle %d %s (%d %d)' % (k,ii == jj,ii,jj)
    

    And for completeness, if you want to go back from the 1d index to the multidimensional index in a fast, vectorized-style way:

    print 'Convert C-style from linear to multi'
    x = box_index_C.reshape(-1,1)
    bassign_rev_C = x / b_C % nbins 
    
    print 'Convert F-style from linear to multi'
    x = box_index_F.reshape(-1,1)
    bassign_rev_F = x / b_F % nbins
    

    and again to check:

    print 'Check C-order'
    for k in xrange(N):
        ii = tuple(binassign[:,k])
        jj = tuple(bassign_rev_C[k,:])
        print ii==jj,ii,jj
    
    print 'Check F-order'
    for k in xrange(N):
        ii = tuple(binassign[:,k])
        jj = tuple(bassign_rev_F[k,:])
        print ii==jj,ii,jj 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.