MATLAB Answers

# delete elements of a vector wich are not consecutive

조회 수: 1(최근 30일)
Fabian Heinzl 2021년 7월 29일
댓글: Fabian Heinzl 2021년 7월 29일
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 2021년 7월 29일
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표시숨기기 이전 댓글 수: 1
Fabian Heinzl 2021년 7월 29일
Never Mind got it, thanks again :)

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

KSSV 2021년 7월 29일
MAke your indices.....and then extract....
Hint: Your indices are in Arithmetic Progression.

댓글을 달려면 로그인하십시오.

### Community Treasure Hunt

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

Start Hunting!

Translated by