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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:31:06+00:00 2026-06-17T23:31:06+00:00

I have two 2D array, x(ni, nj) and y(ni,nj), that I need to interpolate

  • 0

I have two 2D array, x(ni, nj) and y(ni,nj), that I need to interpolate over one axis. I want to interpolate along last axis for every ni.

I wrote

import numpy as np
from scipy.interpolate import interp1d

z = np.asarray([200,300,400,500,600])
out = []
for i in range(ni):
    f = interp1d(x[i,:], y[i,:], kind='linear')
    out.append(f(z))
out = np.asarray(out)

However, I think this method is inefficient and slow due to loop if array size is too large. What is the fastest way to interpolate multi-dimensional array like this? Is there any way to perform linear and cubic interpolation without loop? Thanks.

  • 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-17T23:31:07+00:00Added an answer on June 17, 2026 at 11:31 pm

    The method you propose does have a python loop, so for large values of ni it is going to get slow. That said, unless you are going to have large ni you shouldn’t worry much.

    I have created sample input data with the following code:

    def sample_data(n_i, n_j, z_shape) :
        x = np.random.rand(n_i, n_j) * 1000
        x.sort()
        x[:,0] = 0
        x[:, -1] = 1000
        y = np.random.rand(n_i, n_j)
        z = np.random.rand(*z_shape) * 1000
        return x, y, z
    

    And have tested them with this two versions of linear interpolation:

    def interp_1(x, y, z) :
        rows, cols = x.shape
        out = np.empty((rows,) + z.shape, dtype=y.dtype)
        for j in xrange(rows) :
            out[j] =interp1d(x[j], y[j], kind='linear', copy=False)(z)
        return out
    
    def interp_2(x, y, z) :
        rows, cols = x.shape
        row_idx = np.arange(rows).reshape((rows,) + (1,) * z.ndim)
        col_idx = np.argmax(x.reshape(x.shape + (1,) * z.ndim) > z, axis=1) - 1
        ret = y[row_idx, col_idx + 1] - y[row_idx, col_idx]
        ret /= x[row_idx, col_idx + 1] - x[row_idx, col_idx]
        ret *= z - x[row_idx, col_idx]
        ret += y[row_idx, col_idx]
        return ret
    

    interp_1 is an optimized version of your code, following Dave’s answer. interp_2 is a vectorized implementation of linear interpolation that avoids any python loop whatsoever. Coding something like this requires a sound understanding of broadcasting and indexing in numpy, and some things are going to be less optimized than what interp1d does. A prime example being finding the bin in which to interpolate a value: interp1d will surely break out of loops early once it finds the bin, the above function is comparing the value to all bins.

    So the result is going to be very dependent on what n_i and n_j are, and even how long your array z of values to interpolate is. If n_j is small and n_i is large, you should expect an advantage from interp_2, and from interp_1 if it is the other way around. Smaller z should be an advantage to interp_2, longer ones to interp_1.

    I have actually timed both approaches with a variety of n_i and n_j, for z of shape (5,) and (50,), here are the graphs:

    enter image description here

    enter image description here

    So it seems that for z of shape (5,) you should go with interp_2 whenever n_j < 1000, and with interp_1 elsewhere. Not surprisingly, the threshold is different for z of shape (50,), now being around n_j < 100. It seems tempting to conclude that you should stick with your code if n_j * len(z) > 5000, but change it to something like interp_2 above if not, but there is a great deal of extrapolating in that statement! If you want to further experiment yourself, here’s the code I used to produce the graphs.

    n_s = np.logspace(1, 3.3, 25)
    int_1 = np.empty((len(n_s),) * 2)
    int_2 = np.empty((len(n_s),) * 2)
    z_shape = (5,)
    
    for i, n_i in enumerate(n_s) :
        print int(n_i)
        for j, n_j in enumerate(n_s) :
            x, y, z = sample_data(int(n_i), int(n_j), z_shape)
            int_1[i, j] = min(timeit.repeat('interp_1(x, y, z)',
                                            'from __main__ import interp_1, x, y, z',
                                            repeat=10, number=1))
            int_2[i, j] = min(timeit.repeat('interp_2(x, y, z)',
                                            'from __main__ import interp_2, x, y, z',
                                            repeat=10, number=1))
    
    cs = plt.contour(n_s, n_s, np.transpose(int_1-int_2))
    plt.clabel(cs, inline=1, fontsize=10)
    plt.xlabel('n_i')
    plt.ylabel('n_j')
    plt.title('timeit(interp_2) - timeit(interp_1), z.shape=' + str(z_shape))
    plt.show()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two array and three button in segment control i want that when
I have a two dimensional array that I need to rotate 90 degrees clockwise,
I have a two dimensional array that I need to sort numerically. Here is
I have a two dimensional array in php that I need to express as
In a nutshell I have two images I want to overlay one over the
I have two types of array's fields that I want to sort according to:
I have a two dimensional array that I need to load data into. I
If I have an array of doubles that each have EXACTLY two decimal places,
Assume that I have two arrays as follow: $array1 = array(1, 3, 5); $array2
I have image Array with two images out of that first image its showing

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.