Reputation: 1356
I'm not sure of an easier way to increment a range of numbers to use as an index to extract specific ranges in a series of numbers in a 12-number sequence. For example, I need an index to do the following:
Array1
is an array of 1 row by 612 columns. I need to create an index so that I"m extracting values 8,9,10,11 and 12 and then incrementing those by 12 so that the next columns I extract are 20, 21, 22, 23 and 24 and so forth to column == 612
.
My index will look like this:
index = [ 8 9 10 11 12 20 21 22 23 24 32 33 34 35 36 ]
etc to 612.
I tried using something like index = [ 8:12:12:612]
but it just gives me [ 8 20 32, etc]
.
Upvotes: 2
Views: 310
Reputation: 221524
bsxfun
based approach -
array1 = 8:12; %// Starting array
sz = 12; %// Stepsize
Ncols = floor((size(A,2)-array1(1))/sz)+1 %// No. of blocks of indices
ind1 = bsxfun(@plus,array1.',[0:Ncols-1]*sz) %//' Indices in blocks
index = ind1(ind1<=size(A,2)); %// Valid indices
Example -
A = rand(1,23); %// Random input for demo
array1 = 1:4; %// Starting array
sz = 8; %// Stepsize
Output -
index =
1 2 3 4 9 10 11 12 17 18 19 20
Upvotes: 1
Reputation: 112659
Let
S = 12; %// major step (minor step is 1)
G = 5; %// group size
I = 8; %// initial value
F = 612; %// ending value
Then the indices can be generated with this simple mod
-based approach:
index = find(mod(0:F-1,S)<G)+I-1;
Upvotes: 0