Thread Subject: Interpolation

Subject: Interpolation

From: Jianbin

Date: 16 Jul, 2007 14:45:56

Message: 1 of 7

Does anyone have the code to do interpolation so that it always takes the lower value.

For example, here is a known distribution
X Y
0.01 200
0.02 500
0.03 700
0.04 900
0.05 1000
... ...
0.99 20000
1 20300

if x1=0.015, then y1=200; x1=0.019, y1=200; x1=0.039, y1=700;...


Thanks

Subject: Interpolation

From: Per Sundqvist

Date: 16 Jul, 2007 20:39:02

Message: 2 of 7

help sort. Try this

xd=0.15;%your data point
[dummy,ix]=sort(abs(xd-x));
val_closest=[y(ix(1)) y(ix(2))]
[yy,ix0]=sort(val_clostest);
%yy is the closest smallest value y
yy
%and correspond to closest datapoint xx
xx=x(ix0)

/Per

Subject: Interpolation

From: John D'Errico

Date: 16 Jul, 2007 22:38:26

Message: 3 of 7

> Does anyone have the code to do interpolation so that it always takes the lower value.
>
> For example, here is a known distribution
> X Y
> 0.01 200
> 0.02 500
> 0.03 700
> 0.04 900
> 0.05 1000
> ... ...
> 0.99 20000
> 1 20300
>
> if x1=0.015, then y1=200; x1=0.019, y1=200; x1=0.039, y1=700;...


In your example, its simple.

ind = floor(x1/0.1);
y1 = Y(ind);

If X is not so simply spaced, then you can use
interp1.

ind = floor(interp1(X,1:length(X),x1,'linear'));
y1 = Y(ind);

HTH,
John

Subject: Interpolation

From: us

Date: 17 Jul, 2007 00:36:36

Message: 4 of 7

<SNIP apparent interpolation...

one of the many solutions

% your dist and input
     d=[
          .01 100
          .02 200
          .03 300
          .09 900
     ];
     in=[
          .01
          .011
          .015
          .025
          .07
          .09
          .091
          .1
     ];
% the engine
     [ix,ix]=histc(in,sort([d(:,1);in(end)+1]));
% the result
     r=[in,d(ix,:)]

us

Subject: Interpolation

From: Stephen

Date: 17 Jul, 2007 18:20:08

Message: 5 of 7

Thanks for your help.

I try to write this as a function:

===========================================================
function y1=NoInterp(x,Y,x1)

if x1 < min(x)
      x1=min(x);
elseif x1>max(x)
     x1=max(x);
end

ind = floor(interp1(x,1:length(x),x1,'linear'));
y1 = Y(ind);

===========================================================

When x1 is in the range, the function runs OK. When x1 is out of range, the if statement I added to your code doesn't seem to work properly and the function fails. Could you help me figure out what's wrong here?

Thanks a lot.

Stephen

Subject: Interpolation

From: John D'Errico

Date: 17 Jul, 2007 18:30:33

Message: 6 of 7

> I try to write this as a function:

===========================================================
function y1=NoInterp(x,Y,x1)

if x1 < min(x)
      x1=min(x);
elseif x1>max(x)
     x1=max(x);
end

ind = floor(interp1(x,1:length(x),x1,'linear'));
y1 = Y(ind);

===========================================================

> When x1 is in the range, the function runs OK. When x1 is out of range, the > if statement I added to your code doesn't seem to work properly and the
> function fails. Could you help me figure out what's wrong here?


The problem is that if does not work as you are using
it. An if statement is not "vectorized".

However, why bother? Min and max ARE vectorized.

What does this do for you?

  x1 = min(x1,max(x));

How about this?

  x1 = max(x1,min(x));

John

Subject: Interpolation

From: Stephen

Date: 17 Jul, 2007 18:51:34

Message: 7 of 7

Thanks for the prompt response.

I realized the error in my code as x1 is supposed to be a vector. So I changed the code to be:
===============================================
function y1=NoInterp(x,Y,xd)
xd(xd<min(x))=min(x);
xd(xd>max(x))=max(x);
ind = floor(interp1(x,1:length(x),xd,'linear'));
y1 = Y(ind);
================================================

And it works fine now.

Thanks,
Stephen

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
histc us 16 Jul, 2007 20:42:53
code us 16 Jul, 2007 20:40:08
interpolation John D'Errico 16 Jul, 2007 18:24:58
homework Dan Haeg 16 Jul, 2007 13:52:44
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com