how to create series of indices from vectors of numbers

8 views (last 30 days)
Sorry for the obscure title, I think if i show an example it will be easy to understand. I have a vector of numbers: myvec = [ 1 45 70 ]; and I want to somehow generate a set of indices like this:
output = [1 2 3 4 5 6 7 8 9 10 11 45 46 47 48 49 50 51 52 53 54 55 70 71 72 73 74 75 76 77 78 79 80 ]
if i wrote a for loop, it would look like this:
for( i = 1:length(myvec) ) output = [output myvec(i):myvec(i)+10]; end
is there a simple, vectorized way to do this in matlab? for loops are terribly slow, and I want my code to be as fast as possible. thanks!
Liz

Accepted Answer

Wayne King
Wayne King on 22 Nov 2011
Hi, one way:
input = [1 45 70];
y = arrayfun(@(x) x:x+10,input,'uni',0);
y = cell2mat(y);
  3 Comments
Andrei Bobrov
Andrei Bobrov on 23 Nov 2011
output = reshape(bsxfun(@plus,myvec,(0:10)'),1,[]);

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements 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!