how to create series of indices from vectors of numbers

4 次查看(过去 30 天)
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

采纳的回答

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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Title 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by