2015-04-17 102 views
1

我从尝试做到这一点成功炒。我有一个size(A) = [100 612]矩阵数据A。列数据大约有12个月51个地点,即总共612列。创建索引MATLAB

我需要创建一个索引来选择这个序列中的列; 1:51:612,然后2:51:612等最高51:51:612。最终的阵列应该是100行612列矩阵,并且该序列

Row 1: Columns 1-12=(1,52,103,154,205,256,307,358,409,460,511,562) 
     Columns 13-24=(2,53,104,155,206,257,308,359,410,461,512,563) 
     ... 
     etc to the end of the first row with the last 12 columns with these numbers 
     Columns 601-612=(51,102,153,204,255,306,357,408,459,510,561,612). 

然后重复100次以给出100行。我需要这个作为逻辑索引来提取或重新排序上面给出的A中的原始数据。

回答

4

下面是使用permutereshape

out = A(:,reshape(permute(reshape(1:612,51,[]),[2 1 3]),1,[])); 

一个班轮或者你可以避开permute使用transpose

out = A(:,reshape(reshape(1:612,51,[]).',1,[])); 
+2

似乎像你们终于获得了这三个强大的力量功能很好用! – Divakar

+0

不错的。我应该考虑使用'重塑'。 – eigenchris

+1

@Divakar,eigenchris,所有功劳都归功于你们。没有人教过我MATLAB,只是从你的答案,简介等等中学习。 –

2

下面的代码应该工作:

months = 12; 
sites = 51; 

idx  = 1:sites:months*sites;   %// get array [1,52,103,...,562] 
final_idx = bsxfun(@plus,idx',[0:sites-1]); %'//add offsets to idx 
final_idx = final_idx(:)';     %'//get array elements in a row vector 

A_new = A(:,final_idx);      %// rearrange columns