Is there any more effiecient way to implement this?

2 Ansichten (letzte 30 Tage)
Dimitrios
Dimitrios am 18 Okt. 2014
Bearbeitet: per isakson am 18 Okt. 2014
Using MATLAB:I have a table of lets say x(size(100x1)),y(size(100x1)) and z(size(100x100)) and i use
z1 = interp2(x,y,z,x1,x2)
to find a scalar value that i need for later calculation. I was wondering if there is a more efficient/quicker way to implement this.scatteredInterpolant function could help? I tried unsuccessfully to implement it with scatteredInterpolant
  2 Kommentare
per isakson
per isakson am 18 Okt. 2014
"find a value" &nbsp should I understand one scalar value?
Dimitrios
Dimitrios am 18 Okt. 2014
thanks for the comment.i edit the question.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Mohammad Abouali
Mohammad Abouali am 18 Okt. 2014
Bearbeitet: Mohammad Abouali am 18 Okt. 2014
scatteredInterpolant is suitable for cases where your source grid is scattered. If you were able to use interp2 it means that your source grid is not scattered. Although you can still use scattered interpolant, it does not give you any additional benefit.
Here are two approaches:
1) using gridded interpolant:
[XGrid,YGrid]=meshgrid(x,y);
F=griddedInterpolant(XGrid',YGrid',z','cubic');
Then you can query that for any point as follow:
z1=F(x1,x2);
x1,x2 could be scattered points or gridded. If it is gridded it should follow NDGrid format.
2) Using Scattered Interpolant:
[XGrid,YGrid]=meshgrid(x,y);
F=scatteredInterpolant(XGrid(:), YGrid(:),z(:));
Then you can query that for any point as follow:
z1=F(x1,x2);
  1 Kommentar
Dimitrios
Dimitrios am 18 Okt. 2014
thank you for ur reply.
Tried both approaches.I just changed the cubic to spline.Output value is almost similar(offcurse i didnt expect exactly the same result).I just post the results of computational time:
Interpolation: 5.18sec
griddenInterpolant: 4.8sec
scatteredInterpolant: 10.1sec

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

per isakson
per isakson am 18 Okt. 2014
Bearbeitet: per isakson am 18 Okt. 2014
If linear is good enough as you indicate in the question (default of interp2).
My approach to a similar performance problem included
  • avoid the function call. The call itself takes longer than the computation (of a scalar)
  • a few lines of code, which do the interpolation, directly in the "calling" code.

Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by