Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Python equivalent of Matlab code kwokmaster Unladen Swallow Posts: 4 Threads: 2 Joined: Mar 2020 Reputation: 0 Likes received: 0 #1 Mar-15-2020, 04:58 PM I am converting the interp2 function of matlab to python. interp2 However, after searching through the equivalents like interpolate.interp2d, they all only allow 1-D array input, while my function requires a 2-D array input. I have no knowledge about interpolation and I can't handwrite an equivalent for it. It would be great if anyone can help. Thank you in advance! scidam Posts: 677 Threads: 1 Joined: Mar 2018 Reputation: 90 Likes received: 103 #2 Mar-16-2020, 11:36 AM If you have a meshgrid data, e.g. ```import numpy as np # x, y -- 1D arrays X, Y = np.meshgrid(x, y) # Z is a matrix, ```you can always do `interpolate.interp2d(X.ravel(), Y.ravel(), Z.ravel())`Another option, that should also work, is `interpolate.interp2d(x, y, Z)`It seems (from the docs) that MatLab's interp2 and scipy's interp2d do not differ significantly. kwokmaster Unladen Swallow Posts: 4 Threads: 2 Joined: Mar 2020 Reputation: 0 Likes received: 0 #3 Mar-31-2020, 07:13 PM Hi scidam, thanks for the reply! I tried the way you mentioned like the following ip = interpolate.interp2d(Ui,Vi,im,kind='linear') out = ip(U,V) where U,V is a 1000*1000 array and im is a 701*701 array. Then I return with the following error: ValueError: Invalid length for input z for non rectangular grid As I have no idea about interpolation, what is this error about and how should I solve it? Thank you! scidam Posts: 677 Threads: 1 Joined: Mar 2018 Reputation: 90 Likes received: 103 #4 Apr-01-2020, 01:22 AM I suspect that the problem is in the line `ip = interpolate.interp2d(Ui,Vi,im,kind='linear')`. Could you provide a minimal reproducible example? What are shapes of Ui, Vi? It is hard to answer without additional information. kwokmaster Unladen Swallow Posts: 4 Threads: 2 Joined: Mar 2020 Reputation: 0 Likes received: 0 #5 Apr-01-2020, 07:49 AM (Apr-01-2020, 01:22 AM)scidam Wrote: I suspect that the problem is in the line `ip = interpolate.interp2d(Ui,Vi,im,kind='linear')`. Could you provide a minimal reproducible example? What are shapes of Ui, Vi? It is hard to answer without additional information. The shapes of Ui and Vi are both 701*701 scidam Posts: 677 Threads: 1 Joined: Mar 2018 Reputation: 90 Likes received: 103 #6 Apr-03-2020, 12:02 AM Here is minimal working example, ```>>> from scipy.interpolate import interp2d >>> import numpy as np >>> x, y = np.random.rand(701), np.random.rand(701) >>> z = np.random.rand(701, 701) >>> inp = interp2d(x, y, z)```So, if you call `inp([1,2,3],[4,5,6])`you get a matrix of size 3x3 with values for all combinations of points. Try to restructure U and V into 1D arrays (probably of shape (1000,)). « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Is there similar function to lsqnonlin (in matlab) in python? Jay_Nerella 1 1,152 Nov-11-2019, 08:40 AM Last Post: feli_x Matlab to Python Ariane 6 2,639 Jun-14-2018, 12:08 PM Last Post: Ariane How to use .m matlab file in python ? sameer 5 7,622 May-10-2018, 09:39 AM Last Post: wavic What is the equivalent python code for c program? nvnkumrawat 10 2,215 Jan-09-2018, 07:22 PM Last Post: Windspar Importing matlab cell array (.mat) into a python list scanato 0 4,424 Nov-15-2017, 11:04 AM Last Post: scanato

Forum Jump:

Users browsing this thread: 1 Guest(s)