function M=lpmatrix(X,h,p,x_net,kernel)
% function M=lpmatrix(X,h,p,x_net,kernel)
% This function counts smoothing matrix for local polynomial estimatiors.
% X - data points
% h - bandwidth
% p - order of the local polynomial
%     p=0 - Nadaraya-Watson estimation
%     p=1 - local linear estimation
% x_net  - in theese points will be counted the estimated function
% kernel - name of file containing kernel function; if this parameter is not
%          given, Epanechnikov kernel is used
% --------------------------------------------------------------------------
% For data values Y the estimated function m is given as m=M*Y

if nargin<4
 error('lpmatrix: Too litle input parameters, sorry.');
end
if nargin==4
 kernel=K_def('epan');
end
X=col(X);
[m,n]= size(X);
if n~=1
 error('lpmatrix: Wrong size of data vector.');
end
x_net=row(x_net);
[M,N]=size(x_net);
if M~=1
 error('lpmatrix: Wrong size of mesh vector.');
end
M=[];
for x=x_net
 pomx=X-x;
 w=K_val(kernel,(pomx/h))/h;
 px=ones(m,1);
 Xx=px;
 Xxw=px.*w;
 for k=1:p
  px=px.*pomx;
  Xx=[Xx px];
  Xxw=[Xxw px.*w];
 end
 A=inv(Xxw'*Xx)*Xxw';
 M=[M; A(1,:)];
end
