Interpolating data in a better way?

4 views (last 30 days)
Walter
Walter on 2 Apr 2014
Commented: dpb on 3 Apr 2014
I will simplify this down, as my data is huge. I have about 100 sets of data smaller than the one I am going up to; each one of them vary in size. IE 3x6 or 9x9. I need to interpolate it up to say 20x20. Names of variables were changed for simplicity also.
A 3x3 raw data would look like this, and cannot change (easily) as I am importing from an edit locked file.
rawdata=[1 7 3 ;8 9 6; 2 2 5];
Here is how am currently going about it.
g_raw = size(rawdata);
xg_g = linspace(1,20,g_raw(1));
yg_g = linspace(1,20,g_raw(2));
interp_x_g = interp1(xg_g, g_raw, 1:20); %doing one direction
interp_y_g = interp1(yg_g, interp_x_g', 1:20)'; %tackling other direction with a transpose and transpose back
interpolated_g = interp_y_g(:); %making into a vector for plotting vs another vector
It works in a sense, but comes out very choppy. Is there a better way? I have been using MatLab for about a week and getting the hang of it. I just do not understand everything in the help files. IE I could not get polyfit to work in my loops. Not sure if I should be using interp2 or 3? All help is appreciated, even if it is just correcting syntax or general advice.
Cheers!
Walt

Accepted Answer

dpb
dpb on 3 Apr 2014
Edited: dpb on 3 Apr 2014
Yeah, try interp2 -- sotoo
[nr,nc]=size(rawdata);
[X,Y] = meshgrid(1:nr,1:nc); % the original array mesh
[Xq,Yq] = meshgrid(linspace(1,nr,20),linspace(1,nr,20)); % the 20X copy
Vq = interp2(X,Y,rawdat,Xq,Yq);
  2 Comments
Walter
Walter on 3 Apr 2014
I got that working with some edits. Turns out if you do my method and then yours and plot them against each other you get a one to one relationship. So I guess interp2 is doing exactly the same thing but in a much cleaner manner. Thanks!
dpb
dpb on 3 Apr 2014
So yours was, indeed, correct! :) I didn't read closely enough to tell but yes the two are the same net result.
You mentioned roughness; the alternative would be a smoothing instead of interpolating spline if the beginning points don't have to be reproduced identically.

Sign in to comment.

More Answers (0)

Categories

Find more on Interpolation in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!