/* File: binSearch.c
*
* C source code for MEX version of binary search routine
* G.W. Recktenwald, 5 Jan 1998
*/
#include <math.h>
#include "mex.h"
/* Input Arguments */
#define X_IN prhs[0]
#define XHAT_IN prhs[1]
/* Output Arguments */
#define INDEX_OUT plhs[0]
/* -------- prototypes for functions defined in this file */
unsigned int binSearch(double x[], unsigned int n, double xhat);
/* -------- Begin mexFunction(), the mex gateway to MATLAB -----------*/
void mexFunction(int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[]
)
{
double *i,*x,*xhat;
unsigned int n;
int rows, cols;
/* Check for proper number of arguments */
if (nrhs != 2) {
mexErrMsgTxt("binSearch requires two input arguments.");
} else if (nlhs > 1) {
mexErrMsgTxt("binSearch requires one output argument.");
}
/* Create a matrix for the return argument */
INDEX_OUT = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to the input and output parameters */
i = mxGetPr(INDEX_OUT);
x = mxGetPr(X_IN);
xhat = mxGetPr(XHAT_IN);
rows = mxGetM(X_IN); /* Get number of rows in input matrix */
cols = mxGetN(X_IN); /* Get number of columns */
n = rows * cols; /* Total number of elements */
/* The binSearch does the work and returns an unsigned int,
* which must be cast to double before assigning to i.
* Note that binSearch() returns the zero-offset index.
* Add one to that result to get proper MATLAB index,
* which is unit offset
*/
*i = (double)(1+binSearch(x,n,*xhat));
return;
}
/* ------------------ binSearch() --------------------------- */
/*
* Binary search to find index i such that x[i] <= xhat <= x[i+1]
*
* Input: x[] = (double) vector of monotonic data
* n = (unsigned int) length of x[]
* xhat = test value
*
* Return: i = index in x vector such that x[i]<= xhat <= x[i+1]
*
* Note: This C function returns the index in the zero-offset
* vector x. Add one to the result to obtain the correct
* MATLAB index, which is unit-offset.
*/
unsigned int binSearch(double x[], unsigned int n, double xhat)
{
unsigned int ia,ib,im;
ia = 0; ib = n-1; /* Last element is n-1 for zero-offset vector */
while (ib-ia>1) {
im = (ia+ib) >> 1; /* Right shift by 1 is efficient integer divide by 2 */
if (x[im] < xhat) {
ia = im; /* Replace lower bracket */
} else {
ib = im; /* Replace upper bracket */
}
}
return ia; /* While loop terminates when ia is desired index */
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
Numerical Methods with MATLAB(NMM1[1].5计算方法工具箱).matlab的nmm工具箱,相信很多人都找过哦,Numerical Methods with MATLAB是一本很好很好的matlab入门书,就不用资源分了
资源推荐
资源详情
资源评论
收起资源包目录
Numerical Methods with MATLAB(NMM1[1].5计算方法工具箱). (279个子文件)
.DS_Store.1 6KB
binSearch.c 3KB
gc87.dat 171KB
gc87flow.dat 120KB
traffic.dat 34KB
Tfield.dat 20KB
xy5.dat 10KB
chip.dat 6KB
CorvRain.dat 5KB
xy2.dat 5KB
xy.dat 3KB
xtyt.dat 3KB
stdatm.dat 1KB
wolfSun.dat 1KB
SiC.dat 902B
H2Osat.dat 898B
emission.dat 879B
airSoundSpeed.dat 751B
glycerin.dat 709B
sphereCd.dat 651B
xyline.dat 544B
flowsys2.dat 504B
flowsys1.dat 504B
GPL104.dat 484B
pdxTheadLong.dat 474B
GPL106.dat 464B
GPL102.dat 440B
Jtcouple.dat 426B
GPL100.dat 407B
H2Ovisc.dat 399B
pdxThead.dat 372B
capacitor.dat 336B
thermis.dat 322B
bearing.dat 322B
airSat.dat 309B
cucon1.dat 305B
pdxPass.dat 288B
sprint.dat 264B
pdxTemp.dat 255B
xydy.dat 252B
cucon3.dat 251B
R2.dat 220B
H2Odensity.dat 214B
fan13v.dat 211B
velocity.dat 189B
xinvpx.dat 180B
fan12v.dat 177B
airvisc.dat 162B
vprofile.dat 162B
cucon2.dat 161B
fan11v.dat 160B
fan10v.dat 149B
fan9v.dat 146B
fan8v.dat 132B
fan7v.dat 122B
.DS_Store 6KB
NMMfiles.html 27KB
prenhallBookCover.jpg 28KB
GLagTable.m 24KB
Contents.m 23KB
makeHTMLindex.m 14KB
photoIndex.m 12KB
myArrow3.m 8KB
loadColDateData.m 5KB
Contents.m 5KB
nmmCheck.m 5KB
install.m 4KB
splint.m 4KB
splintFull.m 4KB
loadColData.m 4KB
adaptGK.m 4KB
demoFanCurve.m 4KB
plotSimpInt.m 3KB
Contents.m 3KB
gaussKronrod15.m 3KB
revisionHistory.m 2KB
odeRK4sysv.m 2KB
GLTable.m 2KB
demoSteel.m 2KB
demoAdaptGK.m 2KB
Contents.m 2KB
adaptSimpsonTrace.m 2KB
odeRK4sys.m 2KB
H2Oprops.m 2KB
brackPlot.m 2KB
bisect.m 2KB
splintFE.m 2KB
newtonSys.m 2KB
quadToInfinity.m 2KB
Contents.m 2KB
adaptSimpson.m 2KB
demoPlaneFit.m 2KB
demoWiggle.m 2KB
Contents.m 2KB
compIntRules.m 2KB
luPiv.m 2KB
compSplinePlot.m 2KB
drawPlane.m 2KB
myArrow.m 2KB
Tfield.m 2KB
共 279 条
- 1
- 2
- 3
昔竹
- 粉丝: 4
- 资源: 12
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页