How to speed up a 2D interpolation in MATLAB?

24 ビュー (過去 30 日間)
Jack Smith
Jack Smith 2023 年 1 月 25 日
コメント済み: Jack Smith 2023 年 1 月 30 日
I am reading a table from excel which I am using as the basis for a 2D interpolation, I've included an image of the table with the x, y and output variables shown with the sensitive data omitted for reference.
I have sensor data for position and velocity, with the variable names Raw_Position and Raw_Velocity respetively, that are used to interpolate the Output0 value at a given positon and velocity. The sensor data is in the format of n rows and m columns.
Currently I am achieving this using two interp1 functions inside nested for loops that repeat the calculation for every row and column of the sensor data but this is very slow and my script is taking several minutes to run for a large data set. Is there a way to speed up this process with the interp2 function or something similar?

採用された回答

Paul
Paul 2023 年 1 月 25 日
A single call to interp2 should do the trick
Position0 = 1:4;
Velocity0 = 1:5;
Output0 = Position0.' + Velocity0;
RawPosition =[1 2;3 4;1 2]; RawVelocity = [2 3;4 5;1 2];
Output = interp2(Velocity0,Position0,Output0,RawVelocity,RawPosition)
Output = 3×2
3 5 7 9 2 4
% check
RawPosition + RawVelocity
ans = 3×2
3 5 7 9 2 4
  3 件のコメント
Paul
Paul 2023 年 1 月 25 日
Hi Jack,
As I showed, the RawPosition and RawVelocity inputs to interp2 can be arrays. In my example they were both 3 x 2, as was the output.
Also, according to our sketch Position0 corresponds to rows of Output0 and Velocity0 corresponds to the columns of Output0. In interp2, the x-coordinate corresponds to the columns and the y-coordinate the rows, which is why in my example I had Velocity as x and Postion as y.
Jack Smith
Jack Smith 2023 年 1 月 30 日
Thanks Paul, after swicthing the position and velocity variables this has worked for me now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by