Delete m consecutive rows every n rows

2 views (last 30 days)
Hello! I'm looking to find a way to delete certain rows in a big table that i'm working with. I would like to find a smart way to delete m consecutive rows every n rows. In my case I have m=6 and n=24--> i want rows 25:30 to be erased while rows 31:54 preserved, up till the end. Hope that the question is clear. Thank you!

Accepted Answer

Voss
Voss on 25 Jan 2022
Edited: Voss on 25 Jan 2022
Here's one way to do it, with a matrix. The logic would be the same for a table.
m = 6;
n = 24;
N = 87;
data = (1:N).'+(0:10:20); % some data
disp(data);
1 11 21 2 12 22 3 13 23 4 14 24 5 15 25 6 16 26 7 17 27 8 18 28 9 19 29 10 20 30 11 21 31 12 22 32 13 23 33 14 24 34 15 25 35 16 26 36 17 27 37 18 28 38 19 29 39 20 30 40 21 31 41 22 32 42 23 33 43 24 34 44 25 35 45 26 36 46 27 37 47 28 38 48 29 39 49 30 40 50 31 41 51 32 42 52 33 43 53 34 44 54 35 45 55 36 46 56 37 47 57 38 48 58 39 49 59 40 50 60 41 51 61 42 52 62 43 53 63 44 54 64 45 55 65 46 56 66 47 57 67 48 58 68 49 59 69 50 60 70 51 61 71 52 62 72 53 63 73 54 64 74 55 65 75 56 66 76 57 67 77 58 68 78 59 69 79 60 70 80 61 71 81 62 72 82 63 73 83 64 74 84 65 75 85 66 76 86 67 77 87 68 78 88 69 79 89 70 80 90 71 81 91 72 82 92 73 83 93 74 84 94 75 85 95 76 86 96 77 87 97 78 88 98 79 89 99 80 90 100 81 91 101 82 92 102 83 93 103 84 94 104 85 95 105 86 96 106 87 97 107
idx = (n+1:n+m).'+(n+m)*(0:ceil(N/(n+m))-1); % index of rows to delete
disp(idx);
25 55 85 26 56 86 27 57 87 28 58 88 29 59 89 30 60 90
idx(idx > N) = []; % don't go off the end
disp(idx);
25 26 27 28 29 30 55 56 57 58 59 60 85 86 87
data(idx,:) = []; % delete the dang rows
disp(data);
1 11 21 2 12 22 3 13 23 4 14 24 5 15 25 6 16 26 7 17 27 8 18 28 9 19 29 10 20 30 11 21 31 12 22 32 13 23 33 14 24 34 15 25 35 16 26 36 17 27 37 18 28 38 19 29 39 20 30 40 21 31 41 22 32 42 23 33 43 24 34 44 31 41 51 32 42 52 33 43 53 34 44 54 35 45 55 36 46 56 37 47 57 38 48 58 39 49 59 40 50 60 41 51 61 42 52 62 43 53 63 44 54 64 45 55 65 46 56 66 47 57 67 48 58 68 49 59 69 50 60 70 51 61 71 52 62 72 53 63 73 54 64 74 61 71 81 62 72 82 63 73 83 64 74 84 65 75 85 66 76 86 67 77 87 68 78 88 69 79 89 70 80 90 71 81 91 72 82 92 73 83 93 74 84 94 75 85 95 76 86 96 77 87 97 78 88 98 79 89 99 80 90 100 81 91 101 82 92 102 83 93 103 84 94 104

More Answers (1)

Matt J
Matt J on 25 Jan 2022
Edited: Matt J on 25 Jan 2022
m = 6;
n = 24;
N = 87;
data = (1:N).'+(0:10:20); % some data
discard=repelem( [false;true] , [n;m], ceil(N/(m+n)) );
data(discard(1:N),:)=[]
data = 72×3
1 11 21 2 12 22 3 13 23 4 14 24 5 15 25 6 16 26 7 17 27 8 18 28 9 19 29 10 20 30

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!