Elegant / Vectorized way of getting array from start_index vector and end_index vector?
Show older comments
Hello
Suppose start_index = [1 5 10]; and end_index = [3 8 12];
Is there an elegant solution to get an output of [1 2 3 5 6 7 8 10 11 12], that is the function takes each start value and gives all inclusive values between the start and end value, repeats with the next index, until all inclusive values are included in a single output vector?
Thank you
Accepted Answer
More Answers (1)
start_index = [1 5 10];
end_index = [3 8 12];
index(end_index + 1) = -1; % Implicit pre-allocation
index(start_index) = 1;
result = find(cumsum(index));
In many cases logical indexing is more efficient:
result = (cumsum(index(1:end-1)) == 1);
1 Comment
Azzi Abdelmalek
on 16 Mar 2013
Nice solution
Categories
Find more on Startup and Shutdown 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!