How i can i make the input of interp2 right?

1 view (last 30 days)
jerry x
jerry x on 27 Aug 2013
If i type:
[X,Y] = meshgrid(1:5);
[XI,YI] = meshgrid(1:.1:5);
Z = peaks(X,Y);
ZI = interp2(X,Y,Z,XI,YI);
it is fine.
but if i change into
[X,Y] = meshgrid(1:5);
[XI,YI] = meshgrid(1:.1:5);
Z = rand(25);
ZI = interp2(X,Y,Z,XI,YI);
an error occor:
Error using griddedInterpolant
The size of the grid point arrays should match the size of the value arrays.
Error in interp2/makegriddedinterp (line 220)
F = griddedInterpolant(varargin{:});
Error in interp2 (line 133)
F = makegriddedinterp(X, Y, V, method);
How to deal with it?
  2 Comments
Walter Roberson
Walter Roberson on 27 Aug 2013
The problem does not occur for me. What MATLAB version are you using? What size() are X, Y, Z, XI, YI in the two different csses ?
Image Analyst
Image Analyst on 27 Aug 2013
No errors when I run it either. I suspect you didn't re-run the Z line to get a new Z after you changed the meshgrid line.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 27 Aug 2013
With your edited code, in the failing case, your X and Y are each 5 x 5, but rand(25) will be 25 x 25. Try
Z = rand(size(X));

Tags

Community Treasure Hunt

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

Start Hunting!