Search code examples
matlabrowsequence

How to select 2 rows and skip 3 rows for large datasets in MATLAB?


I am working on my research and my matrix is 1082825x23. I have already separated a table with the column I wanted so it is now a table of 1082825x1. I have looked on Mathworks forum on selecting specific rows and skipping some however the dataset from the forum is rather small so it hasn't been helpful for large datasets.

The closest I have found on Mathworks was this code that I modified

keepRowsIdx = setdiff(4:size(gyro_x, 1), 6:3:size(gyro_x, 1));
new_gyro_x = gyro_x(keepRowsIdx, :);

However it gives the new_gyro_x produces results like the following

0.0047
0.0105
0.0117
0.0234
0.0105
0.0328
etc...

In reality, I am trying to get the values I highlighted in the image below throughout the entire column. I imagine I will need to use a for loop but I'm not sure how to approach that.

enter image description here


Solution

  • One method might be to create two vectors that have the same skipping factor but with unique starting indices. Then concatenate and reshape these two vectors into a single column vector that can be used to index the values stored in gyro_x.

    %Test vector, change to your dataset%
    gyro_x = table((1:1082825).');
    gyro_x = table2array(gyro_x);
    
    Start = 4; End = 15; Skipping_Factor = 5;
    Indices = (Start:Skipping_Factor:End);
    Indices = reshape([Indices; Indices+1],[1 2*numel(Indices)]);
    
    gyro_x(Indices)