One-liner using arrayfun, cell2mat and an anonymous function:
output = cell2mat(arrayfun(@(start, stop) start:stop, a, b, 'uni', 0))
Explanation: the function arrayfun
iterates in parallel over the vectors a
and b
, and then calls the anonymous function for every pair of their elements. The anonymous function returns a vector of varying size instead of a scalar, so you need to use 'UniformOutput', false
(which can be abbreviated to 'uni', 0
) to make arrayfun
return a cell array. Finally, you use cell2mat
to squeeze the cell array together into a vector.
Quick test:
>> a = [10, 20, 40];
>> b = [13, 22, 45];
>> output = cell2mat(arrayfun(@(start, stop) start:stop, a, b, 'uni', 0))
output =
10 11 12 13 20 21 22 40 41 42 43 44 45