Fully vectorized concatenation of multiple ranges?
Show older comments
Hello all,
I've been looking for an efficient way to concatenate an arbitrarily large set of ranges.
For example:
lower_bounds = [1, 5, 20, 50]
upper_bounds = [2, 10, 40, 100]
output = unknown_magic(lower_bounds, upper_bounds)
output =
[1:2 5:10 20:40 50:100]
I've seen similar questions asked, but none addressed this issue directly. The closest question I could find on the Internet was at stackoverflow.
I have found a semi-working solution, which incurs some unnecessary overhead because it uses cells:
output = cell2mat (arrayfun( @(x,y)(x:y), lower_bounds, upper_bounds, 'UniformOutput', false))
However, the arrayfun throws an error ("Non-scalar in Uniform output") unless I pass the argument 'UniformOutput', false. Using this argument incurs the cell array overhead. I use cell2mat to convert back to the concatenated ranges.
If anyone else has a similar problem I hope my makeshift solution works for you until someone posts a better method.
Accepted Answer
More Answers (1)
Guillaume
on 18 Nov 2014
0 votes
I don't think you'll find a more efficient solution. You need a cell array anyway to store all the matrices for each range. The alternative is to store them directly into the final matrix and that would involve multiple resizing since you don't know beforehand the size of the final matrix. This is bound to be slower.
It takes around 2.5 seconds on my machine to process a million bounds. I think that's reasonable.
1 Comment
Michael
on 18 Nov 2014
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!