delete elements of a vector wich are not consecutive

2 visualizaciones (últimos 30 días)
Fabian Heinzl
Fabian Heinzl el 29 de Jul. de 2021
Comentada: Fabian Heinzl el 29 de Jul. de 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 :)

Respuesta aceptada

Rik
Rik el 29 de Jul. de 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 comentarios
Fabian Heinzl
Fabian Heinzl el 29 de Jul. de 2021
Insane Dude! i took me a while to even get what u did there. Awesome. So now i have a Vector wich consists of all Elements i have to get from my initial Vector. Excuse this dumb question but what is the command to get all elements from my initial vector now ? iam kinda burnt out with my task.
Fabian Heinzl
Fabian Heinzl el 29 de Jul. de 2021
Never Mind got it, thanks again :)

Iniciar sesión para comentar.

Más respuestas (1)

KSSV
KSSV el 29 de Jul. de 2021
MAke your indices.....and then extract....
Hint: Your indices are in Arithmetic Progression.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by