How do I select n elements of a sequence in windows of m ? (matlab)

前端 未结 5 1179
温柔的废话
温柔的废话 2021-01-19 10:47

Quick MATLAB question. What would be the best/most efficient way to select a certain number of elements, \'n\' in windows of \'m\'. In other words, I want to select the fir

5条回答
  •  遇见更好的自我
    2021-01-19 11:02

    There's a slight issue with the description of your problem. You say that you want "to select the first 50 elements of a sequence, then elements 10-60..."; however, this would translate to selecting elements:

    • 1-50
    • 10-60
    • 20-70
    • etc.

    That first sequence should be 0-10 to fit the pattern which of course in MATLAB would not make sense since arrays use one-indexing. To address this, the algorithm below uses a variable called startIndex to indicate which element to start the sequence sampling from.

    You could accomplish this in a vectorized way by constructing an index array. Create a vector consisting of the starting indices of each sequence. For reuse sake, I put the length of the sequence, the step size between sequence starts, and the start of the last sequence as variables. In the example you describe, the length of the sequence should be 50, the step size should be 10 and the start of the last sequence depends on the size of the input data and your needs.

    >> startIndex = 10;
    >> sequenceSize = 5;
    >> finalSequenceStart = 20;
    

    Create some sample data:

    >> sampleData = randi(100, 1, 28)
    
    sampleData =
    
      Columns 1 through 18
    
         8    53    10    82    82    73    15    66    52    98    65    81    46    44    83     9    14    18
    
      Columns 19 through 28
    
        40    84    81     7    40    53    42    66    63    30
    

    Create a vector of the start indices of the sequences:

    >> sequenceStart = startIndex:sequenceSize:finalSequenceStart
    
    sequenceStart =
    
        10    15    20
    

    Create an array of indices to index into the data array:

    >> index = cumsum(ones(sequenceSize, length(sequenceStart)))
    
    index =
    
         1     1     1
         2     2     2
         3     3     3
         4     4     4
         5     5     5
    
    >> index = index + repmat(sequenceStart, sequenceSize, 1) - 1
    
    index =
    
        10    15    20
        11    16    21
        12    17    22
        13    18    23
        14    19    24
    

    Finally, use this index array to reference the data array:

    >> sampleData(index)
    
    ans =
    
        98    83    84
        65     9    81
        81    14     7
        46    18    40
        44    40    53
    

提交回复
热议问题