# delete elements of a vector wich are not consecutive

1 view (last 30 days)
Fabian Heinzl on 29 Jul 2021
Commented: Fabian Heinzl on 29 Jul 2021
Hey,
I have a Vector with a lot of values, where i only need the elements 73 to 144. After that I have to skip the next 72 Elements so that the next ones i need are 217 to 288. This continues until the end, so that the mechanism is: get 72 Elements then skip 72 then get 72, and so on. Is it somehow possible to delete the elements not needed as described above ?
Thanks for help :)

Rik on 29 Jul 2021
You need some tricky indexing, but I believe this is what you need:
v=1:(72*6);
k=72;
ind=(k:(2*k):(numel(v)-k)) + (1:k).';
%show as matrix for demo purposes:
ind.'
ans = 3×72
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
ind=ind(:);%linearize to vector
ind(ind>numel(v))=[];%remove trailing indices
v2=v(ind);
##### 2 CommentsShowHide 1 older comment
Fabian Heinzl on 29 Jul 2021
Never Mind got it, thanks again :)

KSSV on 29 Jul 2021
Hint: Your indices are in Arithmetic Progression.