# Vectors most contain unique points interp2

17 Ansichten (letzte 30 Tage)
Bob photonics am 10 Sep. 2020
Beantwortet: Ameer Hamza am 10 Sep. 2020
Matlab is throwing me the following error when using interp2
Error using griddedInterpolant
The grid vectors must contain unique points.
Anyway I can't figure out what I'm doing wrong exactly.
I have a data set, ypos, zpos and T, that I can either plot using scatter3 or converting them to a mesh. I've tried running interp2 with the original data, so 3 arrays (same size) or already gridded data based on the original points.
[YQ,ZQ]=meshgrid(linspace(min(ypos),max(ypos),1001),linspace(min(zpos),max(zpos),1001)); %1001 is just a randomly chosen number for now
Tinterp=interp2(ypos,zpos,T,YQ,ZQ,'spline',0);
%Gives the error of unique points
[YY,ZZ]=meshgrid(ypos,zpos);
TQ = griddata(ypos,zpos,T_f_PFavg,YY,ZZ); %(x,y,v) being your original data for plotting points
Tinterp=interp2(YY,ZZ,TQ,YQ,ZQ,'spline',0); % 'linear' (default) | 'nearest' | 'cubic' | 'spline' | 'makima' (Makima from 2017b on)
%same error as before, again must contain unique point
Now ypos and zpos will have numbers that aren't unique within themselves. HOWEVER they are still unique coordinates when combined. For 2D interpolation it shouldn't matter if X has repeated values, as long as the combination of (X,Y) is unique, in my case (ypos,zpos).
I've added the values for ypos, zpos and T below
ypos=[0 0 0 0 0 100 100 100 100 100 200 200 200 200 200 25 25 25 25 25 300 400 500 50 50 50 50 50 75 75 75 75 75];
zpos=[-10 -15 -20 -30 -5 -10 -15 -20 -30 -5 -10 -15 -20 -30 -5 -10 -15 -20 -30 -5 -30 -30 -30 -10 -15 -20 -30 -5 -10 -15 -20 -30 -5];
T=[0.128191356041225 0.118487402205376 0.109451049557809 0.0935296938065809 0.138542016734843 0.109485248239226 0.101355491616371 0.0937648402043860 0.0802367451462943 0.118489073573599 0.0655846839979417 0.0609182238201411 0.0566150151394948 0.0487568751912317 0.0708555462854102 0.127114032898064 0.117539234308141 0.108606964638423 0.0928073748744567 0.137475054025496 0.0196039219311914 0.00603960758915614 0.00179397880027710 0.123398872383724 0.114118010760545 0.105457140889255 0.0901414785066923 0.133392199123264 0.117289745437074 0.108484124121782 0.100277056331858 0.0857560014061510 0.126787596599440];
If interp2 needs unique points for both X and Y seperately then I would hope someone can point me in the rigth direction to interpolate 2D/3D data. Also I have two version 2016a and 2018 (need to check a/b). I would like to interpolate using the modified Akima method as this seems to give better results, especially when extrapolating (which I need to do a tiny bit).
if anyone is worried about not enough data, this is part of my data, I'm also still waiting on more data, each unique z position takes about 12-24 hours of simulation. Just this data took about 4-5 days to get.
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Ameer Hamza am 10 Sep. 2020
The grid passed to griddata should contain unique elements if you are going to use it with interp2. Try this
ypos=[0 0 0 0 0 100 100 100 100 100 200 200 200 200 200 25 25 25 25 25 300 400 500 50 50 50 50 50 75 75 75 75 75];
zpos=[-10 -15 -20 -30 -5 -10 -15 -20 -30 -5 -10 -15 -20 -30 -5 -10 -15 -20 -30 -5 -30 -30 -30 -10 -15 -20 -30 -5 -10 -15 -20 -30 -5];
T=[0.128191356041225 0.118487402205376 0.109451049557809 0.0935296938065809 0.138542016734843 0.109485248239226 0.101355491616371 0.0937648402043860 0.0802367451462943 0.118489073573599 0.0655846839979417 0.0609182238201411 0.0566150151394948 0.0487568751912317 0.0708555462854102 0.127114032898064 0.117539234308141 0.108606964638423 0.0928073748744567 0.137475054025496 0.0196039219311914 0.00603960758915614 0.00179397880027710 0.123398872383724 0.114118010760545 0.105457140889255 0.0901414785066923 0.133392199123264 0.117289745437074 0.108484124121782 0.100277056331858 0.0857560014061510 0.126787596599440];
[YQ,ZQ]=meshgrid(linspace(min(ypos),max(ypos),1001),linspace(min(zpos),max(zpos),1001)); %1001 is just a randomly chosen number for now
[YY,ZZ]=meshgrid(unique(ypos),unique(zpos));
TQ = griddata(ypos,zpos,T,YY,ZZ);
Tinterp=interp2(YY,ZZ,TQ,YQ,ZQ,'spline',0);
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Interpolation finden Sie in Help Center und File Exchange

R2016a

### Community Treasure Hunt

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

Start Hunting!

Translated by