MATLAB Answers

help with interp2 and interpolation

15 views (last 30 days)
Barry H
Barry H on 16 May 2012
I'm working to write a script which takes a particle's position in the ocean and interpolates its location and the coastline to find the minimum distance to the closest point on coastline.
In debug mode, I find:
size(y_float_init) = 1 189960
size(x_float_init) = 1 189960
size(dist(j,i)) = 1 1
size(dist(:,:) = 541 546
My guess is that I need to reshpae a variable or two, but I'm not sure. Any suggestions/comments are greatly appreciated
It returns the following errors
Error using griddedInterpolant Interpolation requires at least two sample points in each dimension.
Error in interp2/makegriddedinterp (line 220) F = griddedInterpolant(varargin{:});
Error in interp2 (line 125) F = makegriddedinterp({X, Y}, V, method);
Error in float_distance_coast (line 70) dist_offshore_init(j,i) = interp2(X,Y,dist,X1,Y1);
Thanks for the help!
[m n]=size(x_rho);
for i = 1:n
for j = 1:m
x_o = x_rho(j,i);
y_o = y_rho(j,i);
for ii = 1:n
for jj = 1:m
x_1 = x_rho(jj,ii);
y_1 = y_rho(jj,ii);
dist_1(jj,ii) = sqrt(((x_o-x_1)^2)+((y_o-y_1)^2));
ind = find(mask_rho==0);
dist(j,i) = min(dist_1(ind));
x_float = nc_varget(fname, 'x'); %load in trajectories
y_float = nc_varget(fname, 'y'); %Using metric units
%initial position
x_float_init = x_float(49,:);
y_float_init = y_float(49,:);
for i = 1:size(x_float_init)
X = 1:(size(x_float_init));
Y = 1:(size(y_float_init));
[X,Y] = meshgrid(Y,X);
X1 = 1:(size(x_rho));
Y1 = 1:(size(y_rho));
[X1,Y1]= meshgrid(Y1,X1);
dist_offshore_init(j,i) = interp2(X,Y,dist,X1,Y1);


Sign in to comment.

Answers (1)

Patrick on 17 May 2012
What are your m and n ?
And I would use ndgrid and interpn. They don't tranpose like interp2/interp3 and meshgrid, so its alot less confusing.

  1 Comment

Sign in to comment.

Sign in to answer this question.