【问题标题】:Create Index MATLAB创建索引 MATLAB
【发布时间】:2015-04-17 01:08:16
【问题描述】:

尝试成功地做到这一点让我感到很煎熬。我在size(A) = [100 612] 的矩阵中有数据A。列数据由 51 个站点以 12 个月为单位,即总共 612 列。

我需要创建一个索引来选择这个序列中的列; 1:51:612,然后是 2:51:612,等等,直到 51:51:612。最终的数组应该是一个 100 行 x 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 中的原始数据。

【问题讨论】:

    标签: arrays matlab matrix indexing


    【解决方案1】:

    这是一个使用permutereshape 的单行代码

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

    或者你可以通过使用transpose来避免permute

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

    【讨论】:

    • 看来你们终于可以用好这三个强大的功能了!
    • 不错的一个。我应该考虑使用reshape
    • @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 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-18
      • 2011-01-06
      • 2019-02-07
      • 2010-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-20
      相关资源
      最近更新 更多