Inverse distance weighting on scatter data sets in matlab

17 次查看(过去 30 天)
I want to use IDW interpolation technique on my data set.As usual consist on text files. Name Output_00.text to Output_23.text in a folder. Each text file consist on three columns. Latitude, Longitude and Temperature values. 3rd Column( Temperature column) contain -9999.000 value encoded as missing or NaN value. I want to interpolate these NaN/missing value in temperature through inverse distance weighting interpolation technique.
Here is what i am trying
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%INVERSE DISTANCE WEIGHT %%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function[Vint]=IDW(xc,yc,vc,x,y,e,r1,r2)
%%%%%%%%%%%%%%%%%
%%%INPUTS
%xc = stations x coordinates (columns) [vector]
%yc = stations y coordinates (rows) [vector]
%vc = variable values on the point [xc yc]
%x = interpolation points x coordinates [vector]
%y = interpolation points y coordinates [vector]
%e = distance weight
%r1 --- 'fr' = fixed radius ; 'ng' = neighbours
%r2 --- radius lenght if r1 == 'fr' / number of neighbours if r1 =='ng'
%%%OUTPUTS
%Vint --- Matrix [length(y),length(x)] with interpolated variable values
%%%EXAMPLES
%%%--> V_spa=IDW(x1,y1,v1,x,y,-2,'ng',length(x1));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Simone Fatichi -- simonef@dicea.unifi.it
% Copyright 2009
% $Date: 2009/06/19 $
% $Updated 2012/02/24 $
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Vint=zeros(length(y),length(x));
xc=reshape(xc,1,length(xc));
yc=reshape(yc,1,length(yc));
vc=reshape(vc,1,length(vc));
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if strcmp(r1,'fr')
if (r2<=0)
disp('Error: Radius must be positive')
return
end
for i=1:length(x)
for j=1:length(y)
D=[]; V=[]; wV =[]; vcc=[];
D= sqrt((x(i)-xc).^2 +(y(j)-yc).^2);
if min(D)==0
disp('Error: One or more stations have the coordinates of an interpolation point')
return
end
vcc=vc(D<r2); D=D(D<r2);
V = vcc.*(D.^e);
wV = D.^e;
if isempty(D)
V=NaN;
else
V=sum(V)/sum(wV);
end
Vint(j,i)=V;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else
if (r2 > length(vc)) || (r2<1)
disp('Error: Number of neighbours not congruent with data')
return
end
for i=1:length(x)
for j=1:length(y)
D=[]; V=[]; wV =[];vcc=[];
D= sqrt((x(i)-xc).^2 +(y(j)-yc).^2);
if min(D)==0
disp('Error: One or more stations have the coordinates of an interpolation point')
return
end
[D,I]=sort(D);
vcc=vc(I);
V = vcc(1:r2).*(D(1:r2).^e);
wV = D(1:r2).^e;
V=sum(V)/sum(wV);
Vint(j,i)=V;
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
return
I want to modify this code so that it read all text file step by step and interpolate NaN values with IDW interpolation technique and save each text file with IDW_00.text to IDW_23.text in the same folder.
I have attached data in text files. Please help and a lot of thanks for this kind assistance
  2 个评论
Muhammad Usman Saleem
Please suggest me how can i set weight and search radius for interpolation NaN values in my data set. Problem is that NaN values appeared randomly and with out any sequence.I am also not know how many NaN values will be appear in text file. Guide me the best and convenient way of this solution.
Muhammad Usman Saleem
I am confusing in
%x = interpolation points x coordinates [vector]
%y = interpolation points y coordinates [vector]
%e = distance weight
%r1 --- 'fr' = fixed radius ; 'ng' = neighbours
%r2 --- radius lenght if r1 == 'fr' / number of neighbours if r1 =='ng'
How to set x and y coordinates of NaN values? what is r1 and r2?

请先登录,再进行评论。

采纳的回答

Ced
Ced 2016-3-16
编辑:Ced 2016-3-16
I think you should ask yourself first why you want to use this method. What does the method do? What are it's advantages / disadvantages vs a typical cubic method? I would actually even start with a cubic spline, and compare. This should help to build your intuition.
To your questions:
a) How to set x and y coordinates of NaN values:
You already have the x and y coordinates of the NaN values, right? These are the latitude and longitude values. The problem is that you don't know the temperature. And that's what the interpolation is going to give you. So, in your algorithm:
xc, yc, vc: longitude, latitude and temperature of non-nan values.
x, y: longitude and latitude coordinates of your nan-values. This is where you want the algorithm to provide you with an interpolated guess.
b) What is r1 and r2?
From the code, we see the following: The IWD metric computes the distance between your desired interpolation points (in your case your nan values) and the known neighbours around them. The value of the neighbours will be weighted according to this distance. The distance used here is just the euclidean distance for e = 1. The type of norm (i.e. shape of your distance metric) is determined by e. I would start with e = 1.
r1: is used as a flag. There are two modes for this function. The meaning of r2 changes depending on the mode.
if r1 = 'fr', then r2 is the smallest weight that a point must have to be considered for the interpolation. It's the cutoff distance.
if r1 = 'ng', then r2 is the number of closest neighbours used for the interpolation.
Now for the actual values, you will have to tune them a bit to see what works best for you. Initially, you might start with r1 = 'fr', r2 = 10, e = 1.
  23 个评论
Muhammad Usman Saleem
thanks it is finely running.Please i have another query regarding to reading whole text file from a structure. Will you assist me in solution of this query? question is here
Ced
Ced 2016-3-21
Nice! You can "accept" the answer to mark it as solved. I'll have a look at it tonight.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by