extract elements and build the remain vector

14 visualizaciones (últimos 30 días)
Marko
Marko el 21 de Dic. de 2020
Comentada: Marko el 21 de Dic. de 2020
Hello Community,
i need your help.
A vector v has 128 elements with random numbers.
The elements of index 1 21 31 41 should be etracted, and saved as vector a.
the remain elements of vector v should be saved as vector b.
my version consist two for loops which is in deed a bad solution for large vectors.
PS: the vectors are not constant.
maybe someone could finish the algorithm:
v = 1:128;
a = v([1 21 31 41]);

Respuesta aceptada

Image Analyst
Image Analyst el 21 de Dic. de 2020
Use setdiff():
N = 1600;
v = 1:N;
a = [2 59 87 113]
extracted = v(a)
otherIndexes = setdiff(1:length(v), a);
b = v(otherIndexes)

Más respuestas (1)

David Hill
David Hill el 21 de Dic. de 2020
a=v([1 21 31 41]);
b=v([2:20,22:30,32:40,42:end]);
  5 comentarios
David Hill
David Hill el 21 de Dic. de 2020
b=v(~ismember(v,a));
Marko
Marko el 21 de Dic. de 2020
Perfect!
You are a genius!

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices 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