2014-09-03 95 views
2

我不知道的递增号码范围使用作为索引,在一个12-数序列的一系列数字中提取特定范围的更简单的方法。例如,我需要的索引执行以下操作:增量MATLAB范围

Array1是由612列1列的阵列。我需要创建一个索引,以便我提取值8,9,10,11和12,然后将这些值递增12,以便我提取的下一列是20,21,22,23和24等等,以此类推到c 。olumn == 612

我的指数将是这样的:

index = [ 8 9 10 11 12 20 21 22 23 24 32 33 34 35 36 ] 

等,以612

我尝试使用类似index = [ 8:12:12:612],但它只是给我[ 8 20 32, etc]

回答

0

S = 12; %// major step (minor step is 1) 
G = 5; %// group size 
I = 8; %// initial value 
F = 612; %// ending value 

然后,索引可以是根

index = find(mod(0:F-1,S)<G)+I-1; 
:这个简单 mod为基础的方法erated
1

bsxfun为基础的方法 - 。

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 

实施例 -

A = rand(1,23); %// Random input for demo 
array1 = 1:4; %// Starting array 
sz = 8; %// Stepsize 

输出 -

index = 
    1  2  3  4  9 10 11 12 17 18 19 20