Parallelized MATLAB code takes significantly longer time than serial. - matlab

I am wanting to parallelize some MATLAB code that does some parameter fitting using MATLAB's mle routine. My reason for parallelization is that I want to execute the mle routine for multiple different guesses on the same data set.
Initial simulations show that the parallelized code takes anywhere from 200-400 seconds longer than the serial version (~500 seconds) depending on what data set I fit (to time, I just tic and toc at the beginning and end of my code). Am I using parfor incorrectly?
tic
mitdata; % load data
data=dmso; % take the 'DMSO' column data
num=length(data);
mm=linspace(.1, 1, 2)'; % mean values
vv=linspace(.1, 2, 2)'; % variance values
N=length(mm);
n=length(vv);
% get all combinations of the initial parameter guesses I want to try
pp = cell(N, n);
for i = 1:N
for j = 1:n
pp{(i-1)*N+j} = [mm(i), vv(j)];
end
end
temp = nchoosek([1:N*n, 1:N*n], 2);
temp = sort(temp, 2);
idx = unique(temp, 'rows');
P = zeros(length(idx),4);
for ii = 1:length(idx)
P(ii,:) = [pp{idx(ii,1)},pp{idx(ii,2)}];
end
Pcell = num2cell(P); % convert P to cell so that it's easier to assign parameter values for initial guess
options = statset('MaxIter',10000, 'MaxFunEvals',10000);
pd = zeros(length(P),4); % space for best fit parameters
ld = NaN*ones(length(P),1); % space for likelihood values
parfor i=1:length(P)
[m1,v1,m2,v2] = Pcell{i,:};
x0 = [m1,v1,m2,v2]; % initial guess
[p,conf1]=mle(data,'pdf',#convolv_2invG,'start',x0, 'upperbound', [Inf Inf Inf Inf],'lowerbound',[0 0 0 0],'options',options)
pd(i,:)=p; % get best fit parameters from MLE
l=convolv_2invG(data,p(1),p(2),p(3),p(4)); % use best fit parameters to evaluate pdf
l=sum(log(l));
if l<0
ld(i)=l; % store likelihood values
end
end
toc

Related

Creating a heatmap of the logistic map for different values of lambda in matlab

So I am trying to create a heatmap of the logistic map for lambda =2.5 till lambda 4, showing that some outcomes are more common than others as part of my thesis. However so far I did not came far. I plotted the logistic map, however the heatmap part is a bit of a hassle and I can't find how to do it. So, how do I create a heatmap using the coding that I have?
% Logistics Map
% Classic chaos example. Plots semi-stable values of
% x(n+1) = r*x(n)*(1-x(n)) as r increases to 4.
%
clear
scale = 1000; % determines the level of rounding
maxpoints = 200; % determines maximum values to plot
N = 3000; % number of "r" values to simulate
a = 2.5; % starting value of "r"
b = 4; % final value of "r"... anything higher diverges.
rs = linspace(a,b,N); % vector of "r" values
M = 500; % number of iterations of logistics equation
% Loop through the "r" values
for j = 1:length(rs)
r=rs(j); % get current "r"
x=zeros(M,1); % allocate memory
x(1) = 0.5; % initial condition (can be anything from 0 to 1)
for i = 2:M, % iterate
x(i) = r*x(i-1)*(1-x(i-1));
end
% only save those unique, semi-stable values
out{j} = unique(round(scale*x(end-maxpoints:end)));
end
% Rearrange cell array into a large n-by-2 vector for plotting
data = [];
for k = 1:length(rs)
n = length(out{k});
data = [data; rs(k)*ones(n,1),out{k}];
end
% Plot the data
figure(97);clf
h=plot(data(:,1),data(:,2)/scale,'b.');
set(h,'markersize',0.25)
ylim([0 1])
set(gcf,'color','w')
Thanks a lot in advance!

Matlab 'Matrix dimensions must agree' ode23s

The following is my code. I try to model PFR in Matlab using ode23s. It works well with one component irreversible reaction. But when extending more dependent variables, 'Matrix dimensions must agree' problem shows. Have no idea how to fix it. Is possible to use other software to solve similar problems?
Thank you.
function PFR_MA_length
clear all; clc; close all;
function dCdt = df(t,C)
dCdt = zeros(N,2);
dCddt = [0; -vo*diff(C(:,1))./diff(V)-(-kM*C(2:end,1).*C(2:end,2)-kS*C(2:end,1))];
dCmdt = [0; -vo*diff(C(:,2))./diff(V)-(-kM*C(2:end,1).*C(2:end,2))];
dCdt(:,1) = dCddt;
dCdt(:,2) = dCmdt;
end
kM = 1;
kS = 0.5; % assumptions of the rate constants
C0 = [2, 2]; % assumptions of the entering concentration
vo = 2; % volumetric flow rate
volume = 20; % total volume of reactor, spacetime = 10
N = 100; % number of points to discretize the reactor volume on
init = zeros(N,2); % Concentration in reactor at t = 0
init(1,:) = C0; % concentration at entrance
V = linspace(0,volume,N)'; % discretized volume elements, in column form
tspan = [0 20];
[t,C] = ode23s(#(t,C) df(t,C),tspan,init);
end
'''
You can put a break point on the line that computes dCddt and observe that the size of the matrices C and V are different.
>> size(C)
ans =
200 1
>> size(V)
ans =
100 1
The element-wise divide operation, ./, between these two variables would then result in the error that you mentioned.
Per ode23s's help, the output of the call to dCdt = df(t,C) needs to be a vector. However, you are returning a matrix of size 100x2. In the next call to the same function, ode32s converts it to a vector when computing the value of C, hence the size 200x1.
In the GNU octave interpretation of Matlab behavior, one has to explicitly make sure that the solver only sees flat one-dimensional state vectors. These have to be translated forward and back in the application of the model.
Explicitly reading the object A as flat array A(:) forgets the matrix dimension information, these can be added back with the reshape(A,m,n) command.
function dCdt = df(t,C)
C = reshape(C,N,2);
...
dCdt = dCdt(:);
end
...
[t,C] = ode45(#(t,C) df(t,C), tspan, init(:));

Parallel pooling on MATLAB for Bifurcation

I'm new to this concept of parallel pooling on MATLAB (I'm using the version 2019 a) and coding. This code that I'm going to share with you was available on the net, with some few modifications that I've made it for my requirements.
Problem Statement: I'm having a non-linear system (Rossler equation) & I have to plot its Bifurcation diagram, I tried to do it normally using for loop but its computation time was too much and my computer got hanged several times, so I got an advice to parallel pool my code in order to come out of this problem. I tried to learn how to parallel pool using MATLAB on the net but still I'm not able to resolve my Issues as there are still some problems since there are 2 parfor loops in my code I'm having problems with Indexing and in assignment of the global parameter (Please note: This code is written for normal execution without using parallel pooling).
I'm attaching my code below here, please excuse if I've mentioned a lot many lines of codes.
clc;
a = 0.2; b = 0.2; global c;
crange = 1:0.05:90; % Range for parameter c
k = 0; tspan = 0:0.1:500; % Time interval for solving Rossler system
xmax = []; % A matrix for storing the sorted value of x1
for c = crange
f = #(t,x) [-x(2)-x(3); x(1)+a*x(2); b+x(3)*(x(1)-c)];
x0 = [1 1 0]; % initial condition for Rossler system
k = k + 1;
[t,x] = ode45(f,tspan,x0); % call ode() to solve Rossler system
count = find(t>100); % find all the t values which is >10
x = x(count,:);
j = 1;
n = length(x(:,1)); % find the length of vector x1(x in our problem)
for i=2 : n-1
% check for the min value in 1st column of sol matrix
if (x(i-1,1)+eps) < x(i,1) && x(i,1) > (x(i+1,1)+eps)
xmax(k,j)=x(i,1); % Sorting the values of x1 in increasing order
j=j+1;
end
end
% generating bifurcation map by plotting j-1 element of kth row each time
if j>1
plot(c,xmax(k,1:j-1),'k.','MarkerSize',1);
end
hold on;
index(k)=j-1;
end
xlabel('Bifuracation parameter c');
ylabel('x max');
title('Bifurcation diagram for c');
This can be made compatible with parfor by taking a few relatively simple steps. Firstly, parfor workers cannot produce on-screen graphics, so we need to change things to emit a result. In your case, this is not totally trivial since your primary result xmax is being assigned-to in a not-completely-uniform manner - you're assigning different numbers of elements on different loop iterations. Not only that, it appears not to be possible to predict up-front how many columns xmax needs.
Secondly, you need to make some minor changes to the loop iteration to be compatible with parfor, which requires consecutive integer loop iterates.
So, the major change is to have the loop write individual rows of results to a cell array I've called xmax_cell. Outside the parfor loop, it's trivial to convert this back to matrix form.
Putting all this together, we end up with this, which works correctly in R2019b as far as I can tell:
clc;
a = 0.2; b = 0.2;
crange = 1:0.05:90; % Range for parameter c
tspan = 0:0.1:500; % Time interval for solving Rossler system
% PARFOR loop outputs: a cell array of result rows ...
xmax_cell = cell(numel(crange), 1);
% ... and a track of the largest result row
maxNumCols = 0;
parfor k = 1:numel(crange)
c = crange(k);
f = #(t,x) [-x(2)-x(3); x(1)+a*x(2); b+x(3)*(x(1)-c)];
x0 = [1 1 0]; % initial condition for Rossler system
[t,x] = ode45(f,tspan,x0); % call ode() to solve Rossler system
count = find(t>100); % find all the t values which is >10
x = x(count,:);
j = 1;
n = length(x(:,1)); % find the length of vector x1(x in our problem)
this_xmax = [];
for i=2 : n-1
% check for the min value in 1st column of sol matrix
if (x(i-1,1)+eps) < x(i,1) && x(i,1) > (x(i+1,1)+eps)
this_xmax(j) = x(i,1);
j=j+1;
end
end
% Keep track of what's the maximum number of columns
maxNumCols = max(maxNumCols, numel(this_xmax));
% Store this row into the output cell array.
xmax_cell{k} = this_xmax;
end
% Fix up xmax - push each row into the resulting matrix.
xmax = NaN(numel(crange), maxNumCols);
for idx = 1:numel(crange)
this_max = xmax_cell{idx};
xmax(idx, 1:numel(this_max)) = this_max;
end
% Plot
plot(crange, xmax', 'k.', 'MarkerSize', 1)
xlabel('Bifuracation parameter c');
ylabel('x max');
title('Bifurcation diagram for c');

Image corruption in matlab parfor loop

I have some code for an adaptive median filter (see below) which works perfectly until I attempt to run it as a parallel loop, in which case there are values missing or corrupted and the image I get at the end is incorrect (see http://i.stack.imgur.com/Rt6dV.jpg).
Matlab doesn't throw up any errors but appears to be either dropping random data, or overwriting them. The only thing I could find which would even vaguely match this problem was information on custom type definitions and set functions, but all of my input images are uint8 so I don't have any custom set functions or type definitions. (http://fluffynukeit.com/problems-with-matlab-parfor-data-disappearing/)
The error occurs specifically between lines 63 and 86, when I attempt to change
for Index = 1:length(ss)
to
parfor Index = 1:length(ss)
and I can't for the life of me figure out why.
I usually call the function with an image, I, in uint8 format (although any format should work), w_size is an odd number relating to the size of the window for filtering, and M can be set from 0 to 2, where lower numbers reduce the amount of pixels considered when median filtering by increasing the weight of the standard deviation of the current block. So a typical function call would be thus:
I = imread('image.tif');
J = Par_AMF(I, 5, 1);
figure;
imshow(J)
I should finish by saying that typically the size of images I'm using make the parallel processing a huge time saver, usually about half the time of the non-parallel execution, so although there isn't much point for smaller images the ones I'm using would really benefit from it.
function [FIm] = Par_AMF(Im, w_size, M)
% PAR_AMF Adaptive median filter
% Performs a local adaptive median filtering operation
% Inputs: Im - Image to be filtered
% w_size - Window size for filter
% M - Sigma weighting, range from 0 to 2
%
% Output: FIm - Filtered image
tic;
sz_oldim = size(Im);
w_option = floor(w_size/2);
% ----------------------------------------------------------------------- %
% Padding edges of image matrix
Pad_im = padarray(Im,[w_option, w_option],0,'both');
% ----------------------------------------------------------------------- %
% Stage 1
% Calculate the local Mean and Standard Deviation for finding Speckle
% Pixels
% ----------------------------------------------------------------------- %
sz = size(Pad_im);
start_index = w_option+1;
end_index1=(sz(1)-w_option);
end_index2=(sz(2)-w_option);
LB = zeros(sz_oldim);
UB = LB;
first_loop1 = start_index:end_index1;
first_loop2 = start_index:end_index2;
ss = cell(numel(LB), 1);
Index = 1;
for c = first_loop2
for r = first_loop1
ss{Index} = Pad_im(r-w_option:r+w_option,c-w_option:c+w_option);
Index = Index+1;
end
end
% This parfor functions as expected:
parfor Index = 1:length(ss)
ss1=ss{Index}(:);
if any(ss1 > 0)
local_mn = mean(ss1);
local_sig = std(double([ss1;local_mn]));
LB(Index) = local_mn - (M*local_sig);
UB(Index) = local_mn + (M*local_sig);
end
end
% ----------------------------------------------------------------------- %
% UB is the Upper Bound Limit; LB is the Lower Bound Limit
% ----------------------------------------------------------------------- %
% ----------------------------------------------------------------------- %
% Using UB and LB mark pixels as speckle or valid pixels and update only if
% central pixel of window is speckle pixels with median value calculated
% again with only speckle pixels with the window
% ----------------------------------------------------------------------- %
TempOut = Im;
% When this for loop is changed to a parfor (which should definitely work)
% the output is corrupted horribly:
% parfor Index = 1:length(ss)
for Index = 1:length(ss)
ss1 = ss{Index}(:);
ss2 = ss1;
if any(ss1>0)
zz = 0;
for z = 1:length(ss1)
if (ss1(z) < LB(Index) || ss1(z) > UB(Index))
ss1(z) = 0;
ss2(z) = 0;
else
ss1(z)=1;
zz= zz+1;
end
end
if zz > 0
new_ss1 = ss2(ss2~=0);
if(ss1(ceil(length(ss1)/2)) == 0)
md = median(new_ss1);
TempOut(Index) = md;
end
end
end
end
looptime = toc;
disp(['Total processing time ', num2str(looptime./60), ' minutes'])
FIm = TempOut;
end
EDIT: In Matlab version R2015a

Binary Search Using Threshold [duplicate]

I have code of the following kind in MATLAB:
indices = find([1 2 2 3 3 3 4 5 6 7 7] == 3)
This returns 4,5,6 - the indices of elements in the array equal to 3. Now. my code does this sort of thing with very long vectors. The vectors are always sorted.
Therefore, I would like a function which replaces the O(n) complexity of find with O(log n), at the expense that the array has to be sorted.
I am aware of ismember, but for what I know it does not return the indices of all items, just the last one (I need all of them).
For reasons of portability, I need the solution to be MATLAB-only (no compiled mex files etc.)
Here is a fast implementation using binary search. This file is also available on github
function [b,c]=findInSorted(x,range)
%findInSorted fast binary search replacement for ismember(A,B) for the
%special case where the first input argument is sorted.
%
% [a,b] = findInSorted(x,s) returns the range which is equal to s.
% r=a:b and r=find(x == s) produce the same result
%
% [a,b] = findInSorted(x,[from,to]) returns the range which is between from and to
% r=a:b and r=find(x >= from & x <= to) return the same result
%
% For any sorted list x you can replace
% [lia] = ismember(x,from:to)
% with
% [a,b] = findInSorted(x,[from,to])
% lia=a:b
%
% Examples:
%
% x = 1:99
% s = 42
% r1 = find(x == s)
% [a,b] = myFind(x,s)
% r2 = a:b
% %r1 and r2 are equal
%
% See also FIND, ISMEMBER.
%
% Author Daniel Roeske <danielroeske.de>
A=range(1);
B=range(end);
a=1;
b=numel(x);
c=1;
d=numel(x);
if A<=x(1)
b=a;
end
if B>=x(end)
c=d;
end
while (a+1<b)
lw=(floor((a+b)/2));
if (x(lw)<A)
a=lw;
else
b=lw;
end
end
while (c+1<d)
lw=(floor((c+d)/2));
if (x(lw)<=B)
c=lw;
else
d=lw;
end
end
end
Daniel's approach is clever and his myFind2 function is definitely fast, but there are errors/bugs that occur near the boundary conditions or in the case that the upper and lower bounds produce a range outside the set passed in.
Additionally, as he noted in his comment on his answer, his implementation had some inefficiencies that could be improved. I implemented an improved version of his code, which runs faster, while also correctly handling boundary conditions. Furthermore, this code includes more comments to explain what is happening. I hope this helps someone the way Daniel's code helped me here!
function [lower_index,upper_index] = myFindDrGar(x,LowerBound,UpperBound)
% fast O(log2(N)) computation of the range of indices of x that satify the
% upper and lower bound values using the fact that the x vector is sorted
% from low to high values. Computation is done via a binary search.
%
% Input:
%
% x- A vector of sorted values from low to high.
%
% LowerBound- Lower boundary on the values of x in the search
%
% UpperBound- Upper boundary on the values of x in the search
%
% Output:
%
% lower_index- The smallest index such that
% LowerBound<=x(index)<=UpperBound
%
% upper_index- The largest index such that
% LowerBound<=x(index)<=UpperBound
if LowerBound>x(end) || UpperBound<x(1) || UpperBound<LowerBound
% no indices satify bounding conditions
lower_index = [];
upper_index = [];
return;
end
lower_index_a=1;
lower_index_b=length(x); % x(lower_index_b) will always satisfy lowerbound
upper_index_a=1; % x(upper_index_a) will always satisfy upperbound
upper_index_b=length(x);
%
% The following loop increases _a and decreases _b until they differ
% by at most 1. Because one of these index variables always satisfies the
% appropriate bound, this means the loop will terminate with either
% lower_index_a or lower_index_b having the minimum possible index that
% satifies the lower bound, and either upper_index_a or upper_index_b
% having the largest possible index that satisfies the upper bound.
%
while (lower_index_a+1<lower_index_b) || (upper_index_a+1<upper_index_b)
lw=floor((lower_index_a+lower_index_b)/2); % split the upper index
if x(lw) >= LowerBound
lower_index_b=lw; % decrease lower_index_b (whose x value remains \geq to lower bound)
else
lower_index_a=lw; % increase lower_index_a (whose x value remains less than lower bound)
if (lw>upper_index_a) && (lw<upper_index_b)
upper_index_a=lw;% increase upper_index_a (whose x value remains less than lower bound and thus upper bound)
end
end
up=ceil((upper_index_a+upper_index_b)/2);% split the lower index
if x(up) <= UpperBound
upper_index_a=up; % increase upper_index_a (whose x value remains \leq to upper bound)
else
upper_index_b=up; % decrease upper_index_b
if (up<lower_index_b) && (up>lower_index_a)
lower_index_b=up;%decrease lower_index_b (whose x value remains greater than upper bound and thus lower bound)
end
end
end
if x(lower_index_a)>=LowerBound
lower_index = lower_index_a;
else
lower_index = lower_index_b;
end
if x(upper_index_b)<=UpperBound
upper_index = upper_index_b;
else
upper_index = upper_index_a;
end
Note that the improved version of Daniels searchFor function is now simply:
function [lower_index,upper_index] = mySearchForDrGar(x,value)
[lower_index,upper_index] = myFindDrGar(x,value,value);
EDIT many years later: there was an error in the last two if/else blocks, fixed it.
ismember will give you all the indexes if you look at the first output:
>> x = [1 2 2 3 3 3 4 5 6 7 7];
>> [tf,loc]=ismember(x,3);
>> inds = find(tf)
inds =
4 5 6
You just need to use the right order of inputs.
Note that there is a helper function used by ismember that you can call directly:
% ISMEMBC - S must be sorted - Returns logical vector indicating which
% elements of A occur in S
tf = ismembc(x,3);
inds = find(tf);
Using ismembc will save computation time since ismember calls issorted first, but this will omit the check.
Note that newer versions of matlab have a builtin called by builtin('_ismemberoneoutput',a,b) with the same functionality.
Since the above applications of ismember, etc. are somewhat backwards (searching for each element of x in the second argument rather than the other way around), the code is much slower than necessary. As the OP points out, it is unfortunate that [~,loc]=ismember(3,x) only provides the location of the first occurrence of 3 in x, rather than all. However, if you have a recent version of MATLAB (R2012b+, I think), you can use yet more undocumented builtin functions to get the first an last indexes! These are ismembc2 and builtin('_ismemberfirst',searchfor,x):
firstInd = builtin('_ismemberfirst',searchfor,x); % find first occurrence
lastInd = ismembc2(searchfor,x); % find last occurrence
% lastInd = ismembc2(searchfor,x(firstInd:end))+firstInd-1; % slower
inds = firstInd:lastInd;
Still slower than Daniel R.'s great MATLAB code, but there it is (rntmX added to randomatlabuser's benchmark) just for fun:
mean([rntm1 rntm2 rntm3 rntmX])
ans =
0.559204323050486 0.263756852283128 0.000017989974213 0.000153682125682
Here are the bits of documentation for these functions inside ismember.m:
% ISMEMBC2 - S must be sorted - Returns a vector of the locations of
% the elements of A occurring in S. If multiple instances occur,
% the last occurrence is returned
% ISMEMBERFIRST(A,B) - B must be sorted - Returns a vector of the
% locations of the elements of A occurring in B. If multiple
% instances occur, the first occurence is returned.
There is actually reference to an ISMEMBERLAST builtin, but it doesn't seem to exist (yet?).
This is not an answer - I am just comparing the running time of the three solutions suggested by chappjc and Daniel R.
N = 5e7; % length of vector
p = 0.99; % probability
KK = 100; % number of instances
rntm1 = zeros(KK, 1); % runtime with ismember
rntm2 = zeros(KK, 1); % runtime with ismembc
rntm3 = zeros(KK, 1); % runtime with Daniel's function
for kk = 1:KK
x = cumsum(rand(N, 1) > p);
searchfor = x(ceil(4*N/5));
tic
[tf,loc]=ismember(x, searchfor);
inds1 = find(tf);
rntm1(kk) = toc;
tic
tf = ismembc(x, searchfor);
inds2 = find(tf);
rntm2(kk) = toc;
tic
a=1;
b=numel(x);
c=1;
d=numel(x);
while (a+1<b||c+1<d)
lw=(floor((a+b)/2));
if (x(lw)<searchfor)
a=lw;
else
b=lw;
end
lw=(floor((c+d)/2));
if (x(lw)<=searchfor)
c=lw;
else
d=lw;
end
end
inds3 = (b:c)';
rntm3(kk) = toc;
end
Daniel's binary search is very fast.
% Mean of running time
mean([rntm1 rntm2 rntm3])
% 0.631132275892504 0.295233981447746 0.000400786666188
% Percentiles of running time
prctile([rntm1 rntm2 rntm3], [0 25 50 75 100])
% 0.410663611685559 0.175298784336465 0.000012828868032
% 0.429120717937665 0.185935198821797 0.000014539383770
% 0.582281366154709 0.268931132925888 0.000019243302048
% 0.775917520641649 0.385297304740352 0.000026940622867
% 1.063753914942895 0.592429428396956 0.037773746662356
I needed a function like this. Thanks for the post #Daniel!
I worked a little with it because I needed to find several indexes in the same array. I wanted to avoid the overhead of arrayfun (or the like) or calling the function multiple times. So you can pass a bunch of values in range and you will get the indexes in the array.
function idx = findInSorted(x,range)
% Author Dídac Rodríguez Arbonès (May 2018)
% Based on Daniel Roeske's solution:
% Daniel Roeske <danielroeske.de>
% https://github.com/danielroeske/danielsmatlabtools/blob/master/matlab/data/findinsorted.m
range = sort(range);
idx = nan(size(range));
for i=1:numel(range)
idx(i) = aux(x, range(i));
end
end
function b = aux(x, lim)
a=1;
b=numel(x);
if lim<=x(1)
b=a;
end
if lim>=x(end)
a=b;
end
while (a+1<b)
lw=(floor((a+b)/2));
if (x(lw)<lim)
a=lw;
else
b=lw;
end
end
end
I guess you can use a parfor or arrayfun instead. I have not tested myself at what size of range it pays off, though.
Another possible improvement would be to use the previous found indexes (if range is sorted) to decrease the search space. I am skeptical of its potential to save CPU because of the O(log n) runtime.
The final function ended up running slightly faster. I used #randomatlabuser 's framework for that:
N = 5e6; % length of vector
p = 0.99; % probability
KK = 100; % number of instances
rntm1 = zeros(KK, 1); % runtime with ismember
rntm2 = zeros(KK, 1); % runtime with ismembc
rntm3 = zeros(KK, 1); % runtime with Daniel's function
for kk = 1:KK
x = cumsum(rand(N, 1) > p);
searchfor = x(ceil(4*N/5));
tic
range = sort(searchfor);
idx = nan(size(range));
for i=1:numel(range)
idx(i) = aux(x, range(i));
end
rntm1(kk) = toc;
tic
a=1;
b=numel(x);
c=1;
d=numel(x);
while (a+1<b||c+1<d)
lw=(floor((a+b)/2));
if (x(lw)<searchfor)
a=lw;
else
b=lw;
end
lw=(floor((c+d)/2));
if (x(lw)<=searchfor)
c=lw;
else
d=lw;
end
end
inds3 = (b:c)';
rntm2(kk) = toc;
end
%%
function b = aux(x, lim)
a=1;
b=numel(x);
if lim<=x(1)
b=a;
end
if lim>=x(end)
a=b;
end
while (a+1<b)
lw=(floor((a+b)/2));
if (x(lw)<lim)
a=lw;
else
b=lw;
end
end
end
It is not a big improvement, but it helps because I need to run several thousand searches.
% Mean of running time
mean([rntm1 rntm2])
% 9.9624e-05 5.6303e-05
% Percentiles of running time
prctile([rntm1 rntm2], [0 25 50 75 100])
% 3.0435e-05 1.0524e-05
% 3.4133e-05 1.2231e-05
% 3.7262e-05 1.3369e-05
% 3.9111e-05 1.4507e-05
% 0.0027426 0.0020301
I hope this can help somebody.
EDIT
If there is a significant chance of having exact matches, it pays off to use the very fast built-in ismember before calling the function:
[found, idx] = ismember(range, x);
idx(~found) = arrayfun(#(r) aux(x, r), range(~found));