how to create series of indices from vectors of numbers

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

Hi, one way:
input = [1 45 70];
y = arrayfun(@(x) x:x+10,input,'uni',0);
y = cell2mat(y);

3 Comments

of course, I'm not the speed expert, so may Jan or somebody else will way in on a faster way
thanks! that works great =)
output = reshape(bsxfun(@plus,myvec,(0:10)'),1,[]);

Sign in to comment.

More Answers (0)

Categories

Asked:

Liz
on 22 Nov 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!