马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?我要加入
x
有这么一个函数,用来进行插值运算
%a为用户输入的一系列x坐标值
%b为用户输入的一系列y坐标值
%col为1时表示求x=val处的y值,为2时表示求y=val处的x值
%val为值
function y=mminterp(a,b, col, val)
%MMINTERP 1-D Table Search by Linear Interpolation.
%Y=MMINTERP(TAB,COL,VAL) linearly interpolates the table
%TAB searching for the scalar value VAL in the column COL.
%All crossings are found and TAB(:,COL) need not be monotonic.
%Each crossing is returned as a separate row in Y and Y has as
%many columns as TAB.Naturally,the column COL of Y contains
%the value VAL. If VAL is not found in the table,Y=.
%Copyright (c) 1996 by Prentice-Hall,Inc.
tab=[a;b].'
[rt, ct]=size(tab);
if length(val) > 1,error(' VAL must be a scalar. '),end
if col>ct|col < 1,error(' Chosen column outside table width. '),end
if rt < 2,error(' Table too small or not oriented in columns. '),end
above=tab(: , col) > val;%True where > VAL
below=tab(: , col) < val;%True where < VAL
equal=tab(: , col) == val;%True where = VAL
if all(above == 0) | all(below == 0),%handle simplest case
y=tab(find(equal), : );return
end
pslope=find(below(1:rt-1)&above(2:rt));%indices where slope is +
nslope=find(below(2:rt)&above(1:rt-1));%indices where slope is -
ib=sort([pslope; nslope+1]);%put indices below in order
ia=sort([nslope; pslope+1]);%put indices above in order
ie=find(equal);%indices where equal to val
[tmp,ix]=sort( [ib, ie] );%find where equals fit in result
ieq=ix > length(ib);%True where equals values fit
ry=length(tmp);%# of rows in result y
y=zeros(ry, ct);%poke data into a zero matrix
alpha=(val-tab(ib,col))./(tab(ia,col)-tab(ib,col));
alpha=alpha(: , ones(1, ct));%duplicate for all columns
y(~ieq, : )=alpha.*tab(ia, : )+(1-alpha).*tab(ib, : );%interpolated values
y(ieq, : )=tab(ie, : );%equal values
y( : , col)=val*ones(ry, 1);%remove roundoff error
在matlab里调用的时候,输出y似乎是数组的形式,y(1),y(2)都有值
但是做成com组件在vb里面调用时,当我输出msgbox y(1)时提示下标越界
请高手帮忙看看
急 |