MATLAB Answers

delete elements of a vector wich are not consecutive

1 ビュー (過去 30 日間)
Fabian Heinzl
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
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 件のコメント
Fabian Heinzl
Fabian Heinzl 2021 年 7 月 29 日
Never Mind got it, thanks again :)

サインインしてコメントする。

その他の回答 (1 件)

KSSV
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