SlideShare une entreprise Scribd logo
1  sur  3
Télécharger pour lire hors ligne
ts=linspace(0,20,500000);
tic;
for i=1:1000
f=min(find(ts>10));
end;
toc
function [data,x,lower,upper]= find_halfspace_rec(data,x,lower,upper);
mp=floor((lower+upper)/2);
if mp~=lower
if data(mp)<x
[data,x,lower,upper]= find_halfspace_rec(data,x,mp,upper);
else
[data,x,lower,upper]= find_halfspace_rec(data,x,lower,mp);
end;
end;
function l = find_halfspace(data,x);
% returns the number of the last sample in the ASCENDING array data that is
% &lt; x using a simple half space search
upper = numel(data); lower = 1;
[data,x,l,u] = find_halfspace_rec(data,x,lower,upper);
ts=linspace(0,20,500000);
tic;
Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/
第 1 頁,共 3 頁 2015/3/9 下午 06:18
for i=1:1000
f=find_halfspace(ts,10);
end;
toc
#include "mex.h"
/*
* find_halfspace_mex.c
* same as find_halfspace.m
*/
int binary_search(double a[], int low, int high, double target[]) {
int result=-1;
while (low = a[middle])
low = middle + 1;
else
return middle;
result=middle;
}
return result+1;
}
void find_halfspace_mex(double y[], double x[],double f[],int low, int high)
{
y[0]=binary_search(x, low, high, f);
}
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] )
{
double *x,*y, *f;
size_t mrows,ncols,maxsize;
/* Check for proper number of arguments. */
if(nrhs!=2) {
mexErrMsgIdAndTxt( "MATLAB:find_halfspace_mex:invalidNumInputs", "two inputs required."
} else if(nlhs&gt;1) {
mexErrMsgIdAndTxt( "MATLAB:timestwo:maxlhs", "Too many output arguments.");
}
/* The input must be a noncomplex scalar double.*/
mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);
if (mrows&gt;ncols)
maxsize = mrows;
else
maxsize = ncols;
/* Create matrix for the return argument. */
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL);
/* Assign pointers to each input and output. */
x = mxGetPr(prhs[0]);
f = mxGetPr(prhs[1]);
y = mxGetPr(plhs[0]);
if ((mrows==0)||(ncols==0))
y[0]=-1;
else
find_halfspace_mex(y,x,f,0,maxsize);
Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/
第 2 頁,共 3 頁 2015/3/9 下午 06:18
}
ts=linspace(0,20,500000);
tic;
for i=1:1000
f=find_halfspace_mex(ts,10);
end;
toc
Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/
第 3 頁,共 3 頁 2015/3/9 下午 06:18

Contenu connexe

Tendances

benjielloyd1234
benjielloyd1234benjielloyd1234
benjielloyd1234
benjlloyd
 
Aggregators: Data Day Texas, 2015
Aggregators: Data Day Texas, 2015Aggregators: Data Day Texas, 2015
Aggregators: Data Day Texas, 2015
johnynek
 
Exercise roots of equations
Exercise roots of equationsExercise roots of equations
Exercise roots of equations
DUBAN CASTRO
 

Tendances (18)

On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
 
benjielloyd1234
benjielloyd1234benjielloyd1234
benjielloyd1234
 
6. binary tree
6. binary tree6. binary tree
6. binary tree
 
CLUSTERGRAM
CLUSTERGRAMCLUSTERGRAM
CLUSTERGRAM
 
04 stacks
04 stacks04 stacks
04 stacks
 
Java Week10 Notepad
Java Week10   NotepadJava Week10   Notepad
Java Week10 Notepad
 
Lambda Calculus
Lambda CalculusLambda Calculus
Lambda Calculus
 
Linked list part-3
Linked list part-3Linked list part-3
Linked list part-3
 
The simplest existential graph system
The simplest existential graph systemThe simplest existential graph system
The simplest existential graph system
 
Functional programming in Swift
Functional programming in SwiftFunctional programming in Swift
Functional programming in Swift
 
Recursion concepts by Divya
Recursion concepts by DivyaRecursion concepts by Divya
Recursion concepts by Divya
 
Intoduction to numpy
Intoduction to numpyIntoduction to numpy
Intoduction to numpy
 
Aggregators: Data Day Texas, 2015
Aggregators: Data Day Texas, 2015Aggregators: Data Day Texas, 2015
Aggregators: Data Day Texas, 2015
 
Lecture 6: linked list
Lecture 6:  linked listLecture 6:  linked list
Lecture 6: linked list
 
Exercise roots of equations
Exercise roots of equationsExercise roots of equations
Exercise roots of equations
 
R basic programs
R basic programsR basic programs
R basic programs
 
Stack concepts by Divya
Stack concepts by DivyaStack concepts by Divya
Stack concepts by Divya
 
Introduction to the lambda calculus
Introduction to the lambda calculusIntroduction to the lambda calculus
Introduction to the lambda calculus
 

Similaire à Fast lookup in sorted array jakob voigts

関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
Each team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdfEach team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdf
omarionmatzmcwill497
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
michardsonkhaicarr37
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
kesav24
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
stasimus
 

Similaire à Fast lookup in sorted array jakob voigts (20)

Rcommands-for those who interested in R.
Rcommands-for those who interested in R.Rcommands-for those who interested in R.
Rcommands-for those who interested in R.
 
Working with tf.data (TF 2)
Working with tf.data (TF 2)Working with tf.data (TF 2)
Working with tf.data (TF 2)
 
Newton raphson method
Newton raphson methodNewton raphson method
Newton raphson method
 
Implementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 reduxImplementing virtual machines in go & c 2018 redux
Implementing virtual machines in go & c 2018 redux
 
About RNN
About RNNAbout RNN
About RNN
 
About RNN
About RNNAbout RNN
About RNN
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 
The Moore-Spiegel Oscillator
The Moore-Spiegel OscillatorThe Moore-Spiegel Oscillator
The Moore-Spiegel Oscillator
 
Each team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdfEach team works on assigned sorting algorithms and will give demonstr.pdf
Each team works on assigned sorting algorithms and will give demonstr.pdf
 
purrr.pdf
purrr.pdfpurrr.pdf
purrr.pdf
 
Frsa
FrsaFrsa
Frsa
 
A perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdfA perfect left-sided binary tree is a binary tree where every intern.pdf
A perfect left-sided binary tree is a binary tree where every intern.pdf
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
Implement the following sorting algorithms  Bubble Sort  Insertion S.pdfImplement the following sorting algorithms  Bubble Sort  Insertion S.pdf
Implement the following sorting algorithms Bubble Sort Insertion S.pdf
 
Python Lecture 11
Python Lecture 11Python Lecture 11
Python Lecture 11
 
program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)program on string in java Lab file 2 (3-year)
program on string in java Lab file 2 (3-year)
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)
 
Functions in advanced programming
Functions in advanced programmingFunctions in advanced programming
Functions in advanced programming
 
Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.Advanced Data Visualization in R- Somes Examples.
Advanced Data Visualization in R- Somes Examples.
 

Dernier

Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
nirzagarg
 
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
Health
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
gajnagarg
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
chadhar227
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
nirzagarg
 
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
vexqp
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
gajnagarg
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
nirzagarg
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
ptikerjasaptiker
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
wsppdmt
 

Dernier (20)

Dubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls DubaiDubai Call Girls Peeing O525547819 Call Girls Dubai
Dubai Call Girls Peeing O525547819 Call Girls Dubai
 
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
Top profile Call Girls In Bihar Sharif [ 7014168258 ] Call Me For Genuine Mod...
 
Harnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptxHarnessing the Power of GenAI for BI and Reporting.pptx
Harnessing the Power of GenAI for BI and Reporting.pptx
 
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
SR-101-01012024-EN.docx  Federal Constitution  of the Swiss ConfederationSR-101-01012024-EN.docx  Federal Constitution  of the Swiss Confederation
SR-101-01012024-EN.docx Federal Constitution of the Swiss Confederation
 
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
+97470301568>>weed for sale in qatar ,weed for sale in dubai,weed for sale in...
 
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
Top profile Call Girls In Vadodara [ 7014168258 ] Call Me For Genuine Models ...
 
Gartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptxGartner's Data Analytics Maturity Model.pptx
Gartner's Data Analytics Maturity Model.pptx
 
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
怎样办理伦敦大学城市学院毕业证(CITY毕业证书)成绩单学校原版复制
 
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24  Building Real-Time Pipelines With FLaNKDATA SUMMIT 24  Building Real-Time Pipelines With FLaNK
DATA SUMMIT 24 Building Real-Time Pipelines With FLaNK
 
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
Top profile Call Girls In Satna [ 7014168258 ] Call Me For Genuine Models We ...
 
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
怎样办理伦敦大学毕业证(UoL毕业证书)成绩单学校原版复制
 
Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........Switzerland Constitution 2002.pdf.........
Switzerland Constitution 2002.pdf.........
 
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
Top profile Call Girls In Chandrapur [ 7014168258 ] Call Me For Genuine Model...
 
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
Digital Advertising Lecture for Advanced Digital & Social Media Strategy at U...
 
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
Top profile Call Girls In Purnia [ 7014168258 ] Call Me For Genuine Models We...
 
Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...Sequential and reinforcement learning for demand side management by Margaux B...
Sequential and reinforcement learning for demand side management by Margaux B...
 
7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt7. Epi of Chronic respiratory diseases.ppt
7. Epi of Chronic respiratory diseases.ppt
 
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling ManjurJual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
Jual Cytotec Asli Obat Aborsi No. 1 Paling Manjur
 
Discover Why Less is More in B2B Research
Discover Why Less is More in B2B ResearchDiscover Why Less is More in B2B Research
Discover Why Less is More in B2B Research
 
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
一比一原版(UCD毕业证书)加州大学戴维斯分校毕业证成绩单原件一模一样
 

Fast lookup in sorted array jakob voigts

  • 1. ts=linspace(0,20,500000); tic; for i=1:1000 f=min(find(ts>10)); end; toc function [data,x,lower,upper]= find_halfspace_rec(data,x,lower,upper); mp=floor((lower+upper)/2); if mp~=lower if data(mp)<x [data,x,lower,upper]= find_halfspace_rec(data,x,mp,upper); else [data,x,lower,upper]= find_halfspace_rec(data,x,lower,mp); end; end; function l = find_halfspace(data,x); % returns the number of the last sample in the ASCENDING array data that is % &lt; x using a simple half space search upper = numel(data); lower = 1; [data,x,l,u] = find_halfspace_rec(data,x,lower,upper); ts=linspace(0,20,500000); tic; Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/ 第 1 頁,共 3 頁 2015/3/9 下午 06:18
  • 2. for i=1:1000 f=find_halfspace(ts,10); end; toc #include "mex.h" /* * find_halfspace_mex.c * same as find_halfspace.m */ int binary_search(double a[], int low, int high, double target[]) { int result=-1; while (low = a[middle]) low = middle + 1; else return middle; result=middle; } return result+1; } void find_halfspace_mex(double y[], double x[],double f[],int low, int high) { y[0]=binary_search(x, low, high, f); } void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] ) { double *x,*y, *f; size_t mrows,ncols,maxsize; /* Check for proper number of arguments. */ if(nrhs!=2) { mexErrMsgIdAndTxt( "MATLAB:find_halfspace_mex:invalidNumInputs", "two inputs required." } else if(nlhs&gt;1) { mexErrMsgIdAndTxt( "MATLAB:timestwo:maxlhs", "Too many output arguments."); } /* The input must be a noncomplex scalar double.*/ mrows = mxGetM(prhs[0]); ncols = mxGetN(prhs[0]); if (mrows&gt;ncols) maxsize = mrows; else maxsize = ncols; /* Create matrix for the return argument. */ plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); /* Assign pointers to each input and output. */ x = mxGetPr(prhs[0]); f = mxGetPr(prhs[1]); y = mxGetPr(plhs[0]); if ((mrows==0)||(ncols==0)) y[0]=-1; else find_halfspace_mex(y,x,f,0,maxsize); Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/ 第 2 頁,共 3 頁 2015/3/9 下午 06:18
  • 3. } ts=linspace(0,20,500000); tic; for i=1:1000 f=find_halfspace_mex(ts,10); end; toc Fast lookup in sorted array | Jakob Voigts http://jvoigts.scripts.mit.edu/blog/fast-sorted-array-lookup/ 第 3 頁,共 3 頁 2015/3/9 下午 06:18