get pairs / triple / quadruple... of elements from vector by function - algorithm

I have a vector with a couple of elements and I want to write a function that returns me all combinations of x items from this vector.
The following code produces the right output for the case x=2 or x=3 or x=4.
However, I can not implement a solution for every possible x following this idea.
values = {'A','B','C','D','E'};
n = length(values);
data2 = {}; % case x=2
for i = 1:n
for j = i+1:n
data2{end+1} = {values{i}, values{j}};
fprintf('%s %s\n',values{i}, values{j})
end
end
data3 = {}; % case x=3
for i = 1:n
for j = i+1:n
for k = j+1:n
data3{end+1} = {values{i}, values{j}, values{k}};
fprintf('%s %s %s\n',values{i}, values{j}, values{k})
end
end
end
data4 = {}; % case x=4
for i = 1:n
for j = i+1:n
for k = j+1:n
for l = k+1:n
data4{end+1} = {values{i}, values{j}, values{k}, values{l}};
fprintf('%s %s %s %s\n',values{i}, values{j}, values{k}, values{l})
end
end
end
end
How would a function look like which would be able to return my data variable?
data = getCombinations(values, x) %values is vector with elements, x is integer value
EDIT
The following code comes pretty close:
data = perms(values)
data = data(:,1:x)
data = unique(data,'rows')
but it still produces output like A,B and B,A
EDIT2
This fixed it somehow but it is not very nice to look at and it does not work for text entries in cells but only for numbers
data = perms(values)
data = data(:,1:x)
data = sort(data,2)
data = unique(data,'rows')
EDIT3
This did it but it is not very nice to look at... Maybe there is a better solution?
function [data] = getCombinations(values,x)
i = 1:length(values);
d = perms(i);
d = d(:,1:x);
d = sort(d,2);
d = unique(d,'rows');
data = v(d);
end

If you don't want repetitions (and your example suggests you don't) then try nchoosek as nchoosek(1:n, x) to give indices:
values = {'A','B','C','D','E'};
n = length(values);
x = 3;
C = nchoosek(1:n, x);
data = values(C)
In the above, each row is a unique combination of 3 of the 5 elements of values.
Alternatively pass in the values directly:
data = nchoosek(values, x);

Related

Efficiently looping over sub-arrays

I have an array A with size [d1,d2,d3,d4,d5] = size(A).
I have a custom function myFunc that I need to apply to each of the following subarrays of A:
B1 = A(:,1,:,:,:);
B2 = A(:,2,:,:,:);
B3 = A(:,3,:,:,:);
...
The function myFunc has signature:
function [CN1,CN2,CN3,...] = myFunc(BN,varargin)
Here, CN1, CN2, CN3, etc, are a variable number of output arrays whose size depends only on the size of BN.
The goals is to calculate CN1, CN2, CN3, etc, for each BN and then create the arrays
D1 = cat(2,C11,C21,C31,...);
D2 = cat(2,C12,C22,C32,...);
D3 = cat(2,C13,C23,C33,...);
...
I wrote the following function to do this:
function varargout = testFunc(A,funcHdl,varargin)
% Extract the relevant dimensions for array A
sizeA = size(A);
d1 = sizeA(1);
d2 = sizeA(2);
otherDims = sizeA(3:end);
% Extract the number of output arguments of the function handle
numOut = nargout(funcHdl);
Cs = cell(d2,numOut);
% Get the output for all subarrays
for j=1:d2
B = A(:,j,:);
B = reshape(B,[d1,1,otherDims]);
[Cs{j,:}] = funcHdl(B,varargin{:});
end
% Combined the outputs by concatenation
Ds = cell(1,numOut);
for n = 1:numOut
Ds{1,n} = cat(2,Cs{:,n});
end
% Variable number of outputs
varargout = Ds;
end
It's not too inefficient, but I really need this procedure to be efficient. Am I missing a more efficient way of doing this, especially the concatenation?
I use Matlab2020a on Windows 10.

Lua table hash indexing is faster than array indexing in my speed test. Why?

I am doing some tests to see where I can improve the performance of my lua code.
I was reading this document: https://www.lua.org/gems/sample.pdf
and I thought using integers as table indices should be considerably faster since it uses the array part of tables and does not require hashing.
So I've written this test program:
print('local x=0 local y=0 local z=0')
local x=0 local y=0 local z=0
t0 = os.clock()
for i=1,1e7 do
x = 1
y = 2
z = 3
end
print(os.clock()-t0 .. "\n")
print("tab = {1,2,3}")
tab = {1,2,3}
t0 = os.clock()
for i=1,1e7 do
tab[1] = 1
tab[2] = 2
tab[3] = 3
end
print(os.clock()-t0 .. "\n")
print("tab = {[1]=1,[2]=2,[3]=3}")
tab = {[1]=1,[2]=2,[3]=3}
t0 = os.clock()
for i=1,1e7 do
tab[1] = 1
tab[2] = 2
tab[3] = 3
end
print(os.clock()-t0 .. "\n")
print("tab = {a=1,b=2,c=3}")
tab = {a=1,b=2,c=3}
t0 = os.clock()
for i=1,1e7 do
tab.a = 1
tab.b = 2
tab.c = 3
end
print(os.clock()-t0 .. "\n")
print('tab = {["bli"]=1,["bla"]=2,["blu"]=3}')
tab = {["bli"]=1,["bla"]=2,["blu"]=3}
t0 = os.clock()
for i=1,1e7 do
tab["bli"] = 1
tab["bla"] = 2
tab["blu"] = 3
end
print(os.clock()-t0 .. "\n")
print("tab = {verylongfieldname=1,anotherevenlongerfieldname=2,superincrediblylongfieldname=3}")
tab = {verylongfieldname=1,anotherevenlongerfieldname=2,superincrediblylongfieldname=3}
t0 = os.clock()
for i=1,1e7 do
tab.verylongfieldname = 1
tab.anotherevenlongerfieldname = 2
tab.superincrediblylongfieldname = 3
end
print(os.clock()-t0 .. "\n")
print('local f = function(p1, p2, p3)')
local f = function(p1, p2, p3)
x = p1
y = p2
z = p3
return x,y,z
end
local a=0
local b=0
local c=0
t0 = os.clock()
for i=1,1e7 do
a,b,c = f(1,2,3)
end
print(os.clock()-t0 .. "\n")
print('local g = function(params)')
local g = function(params)
x = params.p1
y = params.p2
z = params.p3
return {x,y,z}
end
t0 = os.clock()
for i=1,1e7 do
t = g{p1=1, p2=2, p3=3}
end
print(os.clock()-t0 .. "\n")
I've ordered the blocks by what I expected to be increasing time consumption. (I wasn't sure about the function calls, that was just a test.) But here are the surprising results:
local x=0 local y=0 local z=0
0.093613
tab = {1,2,3}
0.678514
tab = {[1]=1,[2]=2,[3]=3}
0.83678
tab = {a=1,b=2,c=3}
0.62888
tab = {["bli"]=1,["bla"]=2,["blu"]=3}
0.733916
tab = {verylongfieldname=1,anotherevenlongerfieldname=2,superincrediblylongfieldname=3}
0.536726
local f = function(p1, p2, p3)
0.475592
local g = function(params)
3.576475
And even the long field names that should cause the longest hashing process are faster than array accessing with integers. Am I doing something wrong?
The 6th page(actual page 20) of the document you linked explains what you are seeing.
If you write something like {[1] = true, [2] = true, [3] = true}, however, Lua is not smart enough to detect that the given expressions (literal numbers, in this case) describe array indices, so it creates a table with four slots in
its hash part, wasting memory and CPU time.
You can only gain a major benefit of the array part when you assign a table using no keys.
table = {1,2,3}
If you are reading/writing to a table or array that already exists you will not see a large deviation in processing time.
The example in the document includes the creation of the table in the for loop
for i = 1, 1000000 do
local a = {true, true, true}
a[1] = 1; a[2] = 2; a[3] = 3
end
Results with all local variables inside the loops. Edit: Lengthened long string to 40 bytes as pointed out by siffiejoe
local x=0 local y=0 local z=0
0.18
tab = {1,2,3}
3.089
tab = {[1]=1,[2]=2,[3]=3}
4.59
tab = {a=1,b=2,c=3}
3.79
tab = {["bli"]=1,["bla"]=2,["blu"]=3}
3.967
tab = {verylongfieldnameverylongfieldnameverylongfieldname=1,anotherevenlongerfieldnameanotherevenlongerfieldname=2,superincrediblylongfieldnamesuperincrediblylongfieldname=3}
4.013
local f = function(p1, p2, p3)
1.238
local g = function(params)
6.325
Additionally lua preforms the hashes differently for different key types.
The source code can be viewed here 5.2.4 ltable.c, this contains the code I will be discussing.
The mainposition function handles that decision making on which hash to preform
/*
** returns the `main' position of an element in a table (that is, the index
** of its hash value)
*/
static Node *mainposition (const Table *t, const TValue *key) {
switch (ttype(key)) {
case LUA_TNUMBER:
return hashnum(t, nvalue(key));
case LUA_TLNGSTR: {
TString *s = rawtsvalue(key);
if (s->tsv.extra == 0) { /* no hash? */
s->tsv.hash = luaS_hash(getstr(s), s->tsv.len, s->tsv.hash);
s->tsv.extra = 1; /* now it has its hash */
}
return hashstr(t, rawtsvalue(key));
}
case LUA_TSHRSTR:
return hashstr(t, rawtsvalue(key));
case LUA_TBOOLEAN:
return hashboolean(t, bvalue(key));
case LUA_TLIGHTUSERDATA:
return hashpointer(t, pvalue(key));
case LUA_TLCF:
return hashpointer(t, fvalue(key));
default:
return hashpointer(t, gcvalue(key));
}
}
When the key is a Lua_Number we call hashnum
/*
** hash for lua_Numbers
*/
static Node *hashnum (const Table *t, lua_Number n) {
int i;
luai_hashnum(i, n);
if (i < 0) {
if (cast(unsigned int, i) == 0u - i) /* use unsigned to avoid overflows */
i = 0; /* handle INT_MIN */
i = -i; /* must be a positive value */
}
return hashmod(t, i);
}
Here are the other hash implementations for the other types:
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
#define hashstr(t,str) hashpow2(t, (str)->tsv.hash)
#define hashboolean(t,p) hashpow2(t, p)
/*
** for some types, it is better to avoid modulus by power of 2, as
** they tend to have many 2 factors.
*/
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
#define hashpointer(t,p) hashmod(t, IntPoint(p))
These hashes resolve down to 2 paths hashpow2 and hashmod. LUA_TNUMBER use hashnum > hashmod and LUA_TSHRSTR use hashstr > hashpow2

Reduce the calculation time for the matlab code

To calculate an enhancement function for an input image I have written the following piece of code:
Ig = rgb2gray(imread('test.png'));
N = numel(Ig);
meanTotal = mean2(Ig);
[row,cal] = size(Ig);
IgTransformed = Ig;
n = 3;
a = 1;
b = 1;
c = 1;
k = 1;
for ii=2:row-1
for jj=2:cal-1
window = Ig(ii-1:ii+1,jj-1:jj+1);
IgTransformed(ii,jj) = ((k*meanTotal)/(std2(window) + b))*abs(Ig(ii,jj)-c*mean2(window)) + mean2(window).^a;
end
end
How can I reduce the calculation time?
Obviously, one of the factors is the small window (3x3) that should be made in the loop each time.
Here you go -
Igd = double(Ig);
std2v = colfilt(Igd, [3 3], 'sliding', #std);
mean2v = conv2(Igd,ones(3),'same')/9;
Ig_out = uint8((k*meanTotal)./(std2v + b).*abs(Igd-cal*mean2v) + mean2v.^a);
This will change the boundary elements too, which if not desired could be set back to the original ones with few additional steps, like so -
Ig_out(:,[1 end]) = Ig(:,[1 end])
Ig_out([1 end],:) = Ig([1 end],:)

Solve wrong type argument 'cell'

I write in variable 'O' some values using
for i = 1:size(I,1)
for j = 1:size(1,I)
h = i * j;
O{h} = I(i, j) * theta(h);
end
end
I - double, theta - double.
I need to sum()all 'O' values, but when I do it its give me error: sum: wrong type argument 'cell'.
How can I sum() it?
P.s. when I want to see O(), its give me
O =
{
[1,1] = 0.0079764
[1,2] = 0.0035291
[1,3] = 0.0027539
[1,4] = 0.0034392
[1,5] = 0.017066
[1,6] = 0.0082958
[1,7] = 1.4764e-04
[1,8] = 0.0024597
[1,9] = 1.1155e-04
[1,10] = 0.0010342
[1,11] = 0.0039654
[1,12] = 0.0047713
[1,13] = 0.0054305
[1,14] = 3.3794e-04
[1,15] = 0.014323
[1,16] = 0.0026826
[1,17] = 0.013864
[1,18] = 0.0097778
[1,19] = 0.0058029
[1,20] = 0.0020726
[1,21] = 0.0016430
etc...
The exact answer to your question is to use cell2mat
sum (cell2mat (your_cell_o))
However, this is the very wrong way to solve your problem. The thing is that you should not have created a cell array in first place. You should have created a numeric array:
O = zeros (size (I), class (I));
for i = 1:rows (I)
for j = 1:columns (I)
h = i * j;
O(h) = I(i, j) * theta(h);
endfor
endfor
but even this is just really bad and slow. Octave is a language to vectorize operations. Instead, you should have:
h = (1:rows (I))' .* (1:columns (I)); # automatic broadcasting
O = I .* theta (h);
which assumes your function theta behaves properly and if givena matrix will compute the value for each of the element of h and return something of the same size.
If you get an error about wrong sizes, I will guess you have an old version of Octave that does not perform automatic broadcasting. If so, update Octave. If you really can't, then:
h = bsxfun (#times, (1:rows (I))', 1:columns (I));

Run-length decoding in MATLAB

For clever usage of linear indexing or accumarray, I've sometimes felt the need to generate sequences based on run-length encoding. As there is no built-in function for this, I am asking for the most efficient way to decode a sequence encoded in RLE.
Specification:
As to make this a fair comparison I would like to set up some specifications for the function:
If optional second argument values of same length is specified, the output should be according to those values, otherwise just the values 1:length(runLengths).
Gracefully handle:
zeros in runLengths
values being a cell array.
Output vector should have same column/row format as runLengths
In short: The function should be equivalent to the following code:
function V = runLengthDecode(runLengths, values)
[~,V] = histc(1:sum(runLengths), cumsum([1,runLengths(:).']));
if nargin>1
V = reshape(values(V), 1, []);
end
V = shiftdim(V, ~isrow(runLengths));
end
Examples:
Here are a few test cases
runLengthDecode([0,1,0,2])
runLengthDecode([0,1,0,4], [1,2,4,5].')
runLengthDecode([0,1,0,2].', [10,20,30,40])
runLengthDecode([0,3,1,0], {'a','b',1,2})
and their output:
>> runLengthDecode([0,1,0,2])
ans =
2 4 4
>> runLengthDecode([0,1,0,4], [1,2,4,5].')
ans =
2 5 5 5 5
>> runLengthDecode([0,1,0,2].', [10,20,30,40])
ans =
20
40
40
>> runLengthDecode([0,3,1,0],{'a','b',1,2})
ans =
'b' 'b' 'b' [1]
To find out which one is the most efficient solution, we provide a test-script that evaluates the performance. The first plot depicts runtimes for growing length of the vector runLengths, where the entries are uniformly distributed with maximum length 200. A modification of gnovice's solution is the fastest, with Divakar's solution being second place.
This second plot uses nearly the same test data except it includes an initial run of length 2000. This mostly affects both bsxfun solutions, whereas the other solutions will perform quite similarly.
The tests suggest that a modification of gnovice's original answer will be the most performant.
If you want to do the speed comparison yourself, here's the code used to generate the plot above.
function theLastRunLengthDecodingComputationComparisonYoullEverNeed()
Funcs = {#knedlsepp0, ...
#LuisMendo1bsxfun, ...
#LuisMendo2cumsum, ...
#gnovice3cumsum, ...
#Divakar4replicate_bsxfunmask, ...
#knedlsepp5cumsumaccumarray
};
%% Growing number of runs, low maximum sizes in runLengths
ns = 2.^(1:25);
paramGenerators{1} = arrayfun(#(n) #(){randi(200,n,1)}, ns,'uni',0);
paramGenerators{2} = arrayfun(#(n) #(){[2000;randi(200,n,1)]}, ns,'uni',0);
for i = 1:2
times = compareFunctions(Funcs, paramGenerators{i}, 0.5);
finishedComputations = any(~isnan(times),2);
h = figure('Visible', 'off');
loglog(ns(finishedComputations), times(finishedComputations,:));
legend(cellfun(#func2str,Funcs,'uni',0),'Location','NorthWest','Interpreter','none');
title('Runtime comparison for run length decoding - Growing number of runs');
xlabel('length(runLengths)'); ylabel('seconds');
print(['-f',num2str(h)],'-dpng','-r100',['RunLengthComparsion',num2str(i)]);
end
end
function times = compareFunctions(Funcs, paramGenerators, timeLimitInSeconds)
if nargin<3
timeLimitInSeconds = Inf;
end
times = zeros(numel(paramGenerators),numel(Funcs));
for i = 1:numel(paramGenerators)
Params = feval(paramGenerators{i});
for j = 1:numel(Funcs)
if max(times(:,j))<timeLimitInSeconds
times(i,j) = timeit(#()feval(Funcs{j},Params{:}));
else
times(i,j) = NaN;
end
end
end
end
%% // #################################
%% // HERE COME ALL THE FANCY FUNCTIONS
%% // #################################
function V = knedlsepp0(runLengths, values)
[~,V] = histc(1:sum(runLengths), cumsum([1,runLengths(:).']));%'
if nargin>1
V = reshape(values(V), 1, []);
end
V = shiftdim(V, ~isrow(runLengths));
end
%% // #################################
function V = LuisMendo1bsxfun(runLengths, values)
nn = 1:numel(runLengths);
if nargin==1 %// handle one-input case
values = nn;
end
V = values(nonzeros(bsxfun(#times, nn,...
bsxfun(#le, (1:max(runLengths)).', runLengths(:).'))));
if size(runLengths,1)~=size(values,1) %// adjust orientation of output vector
V = V.'; %'
end
end
%% // #################################
function V = LuisMendo2cumsum(runLengths, values)
if nargin==1 %// handle one-input case
values = 1:numel(runLengths);
end
[ii, ~, jj] = find(runLengths(:));
V(cumsum(jj(end:-1:1))) = 1;
V = values(ii(cumsum(V(end:-1:1))));
if size(runLengths,1)~=size(values,1) %// adjust orientation of output vector
V = V.'; %'
end
end
%% // #################################
function V = gnovice3cumsum(runLengths, values)
isColumnVector = size(runLengths,1)>1;
if nargin==1 %// handle one-input case
values = 1:numel(runLengths);
end
values = reshape(values(runLengths~=0),1,[]);
if isempty(values) %// If there are no runs
V = []; return;
end
runLengths = nonzeros(runLengths(:));
index = zeros(1,sum(runLengths));
index(cumsum([1;runLengths(1:end-1)])) = 1;
V = values(cumsum(index));
if isColumnVector %// adjust orientation of output vector
V = V.'; %'
end
end
%% // #################################
function V = Divakar4replicate_bsxfunmask(runLengths, values)
if nargin==1 %// Handle one-input case
values = 1:numel(runLengths);
end
%// Do size checking to make sure that both values and runlengths are row vectors.
if size(values,1) > 1
values = values.'; %//'
end
if size(runLengths,1) > 1
yes_transpose_output = false;
runLengths = runLengths.'; %//'
else
yes_transpose_output = true;
end
maxlen = max(runLengths);
all_values = repmat(values,maxlen,1);
%// OR all_values = values(ones(1,maxlen),:);
V = all_values(bsxfun(#le,(1:maxlen)',runLengths)); %//'
%// Bring the shape of V back to the shape of runlengths
if yes_transpose_output
V = V.'; %//'
end
end
%% // #################################
function V = knedlsepp5cumsumaccumarray(runLengths, values)
isRowVector = size(runLengths,2)>1;
%// Actual computation using column vectors
V = cumsum(accumarray(cumsum([1; runLengths(:)]), 1));
V = V(1:end-1);
%// In case of second argument
if nargin>1
V = reshape(values(V),[],1);
end
%// If original was a row vector, transpose
if isRowVector
V = V.'; %'
end
end
Approach 1
This should be reasonably fast. It uses
bsxfun to create a matrix of size numel(runLengths)xnumel(runLengths), so it may not be suitable for huge input sizes.
function V = runLengthDecode(runLengths, values)
nn = 1:numel(runLengths);
if nargin==1 %// handle one-input case
values = nn;
end
V = values(nonzeros(bsxfun(#times, nn,...
bsxfun(#le, (1:max(runLengths)).', runLengths(:).'))));
if size(runLengths,1)~=size(values,1) %// adjust orientation of output vector
V = V.';
end
Approach 2
This approach, based on cumsum, is an adaptation of that used in this other answer. It uses less memory than approach 1.
function V = runLengthDecode2(runLengths, values)
if nargin==1 %// handle one-input case
values = 1:numel(runLengths);
end
[ii, ~, jj] = find(runLengths(:));
V(cumsum(jj(end:-1:1))) = 1;
V = values(ii(cumsum(V(end:-1:1))));
if size(runLengths,1)~=size(values,1) %// adjust orientation of output vector
V = V.';
end
As of R2015a, the function repelem is the best choice to do this:
function V = runLengthDecode(runLengths, values)
if nargin<2
values = 1:numel(runLengths);
end
V = repelem(values, runLengths);
end
For versions before R2015a this is a fast alternative:
Alternative to repelem:
I had the feeling gnovice's approach could be improved upon by using a better zero-runLength fix than the preprocessing mask.
So I gave accumarray a shot. It seems this gives the solution yet another boost:
function V = runLengthDecode(runLengths, values)
%// Actual computation using column vectors
V = cumsum(accumarray(cumsum([1; runLengths(:)]), 1));
V = V(1:end-1);
%// In case of second argument
if nargin>1
V = reshape(values(V),[],1);
end
%// If original was a row vector, transpose
if size(runLengths,2)>1
V = V.'; %'
end
end
The solution presented here basically does the run-length decoding in two steps -
Replicate all values upto the maximum number of runLengths.
Use bsxfun's masking capability to select from each column the corresponding runlengths.
Rest of the stuffs inside the function code are to take care of the input and output sizes to satisfy the requirements set in the question.
The function code listed next would be a "cleaned-up" version of one of my previous answers to a similar problem. Here's the code -
function V = replicate_bsxfunmask(runLengths, values)
if nargin==1 %// Handle one-input case
values = 1:numel(runLengths);
end
%// Do size checking to make sure that both values and runlengths are row vectors.
if size(values,1) > 1
values = values.'; %//'
end
if size(runLengths,1) > 1
yes_transpose_output = false;
runLengths = runLengths.'; %//'
else
yes_transpose_output = true;
end
maxlen = max(runLengths);
all_values = repmat(values,maxlen,1);
%// OR all_values = values(ones(1,maxlen),:);
V = all_values(bsxfun(#le,(1:maxlen)',runLengths)); %//'
%// Bring the shape of V back to the shape of runlengths
if yes_transpose_output
V = V.'; %//'
end
return;
The code listed next would be a hybrid (cumsum + replicate_bsxfunmask) and would be suitable when you have a good number of outliers or really huge outliers. Also to keep it simple, for now this works on numeric arrays only. Here's the implementation -
function out = replicate_bsxfunmask_v2(runLengths, values)
if nargin==1 %// Handle one-input case
values = 1:numel(runLengths);
end
if size(values,1) > 1
values = values.'; %//'
end
if size(runLengths,1) > 1
yes_transpose_output = true;
runLengths = runLengths.'; %//'
else
yes_transpose_output = false;
end
%// Regularize inputs
values = values(runLengths>0);
runLengths = runLengths(runLengths>0);
%// Main portion of code
thresh = 200; %// runlengths threshold that are to be processed with cumsum
crunLengths = cumsum(runLengths); %%// cumsums of runlengths
mask = runLengths >= thresh; %// mask of runlengths above threshold
starts = [1 crunLengths(1:end-1)+1]; %// starts of each group of runlengths
mask_ind = find(mask); %// indices of mask
post_mark = starts(mask);
negt_mark = crunLengths(mask)+1;
if ~isempty(negt_mark) && negt_mark(end) > crunLengths(end)
negt_mark(end) = [];
end
%// Create array & set starts markers for starts of runlengths above thresh
marked_out = zeros(1,crunLengths(end));
marked_out(post_mark) = mask_ind;
marked_out(negt_mark) = marked_out(negt_mark) -1*mask_ind(1:numel(negt_mark));
%// Setup output array with the cumsumed version of marked array
out = cumsum(marked_out);
%// Mask for final ouput to decide between large and small runlengths
thresh_mask = out~=0;
%// Fill output array with cumsum and then rep-bsxfun based approaches
out(thresh_mask) = values(out(thresh_mask));
values = values(~mask);
runLengths = runLengths(~mask);
maxlen = max(runLengths);
all_values = repmat(values,maxlen,1);
out(~thresh_mask) = all_values(bsxfun(#le,(1:maxlen)',runLengths)); %//'
if yes_transpose_output
out = out.'; %//'
end
return;

Resources