How to delete repeated values in a vector as well as values in its corresponding vector
Mostrar comentarios más antiguos
Hello there!
I am attempting to delete repeated values in a vector and then delete values in a vector of the exact same length at the same index. For instance, say I have the following two vectors:
n_vector = [1 2 3 5 5 5 5 7 6 6];
t_vector = [1 2 3 4 5 6 7 8 9 10];
The n vector has the repeating values 5 and 6, so I need a bit of code that transforms these vectors into this:
n_vector = [1 2 3 5 7 6];
t_vector = [1 2 3 4 8 9];
I tried to do this with the following code, however it doesn't work if values repeat more than twice:
u = 1;
while u > 0
for i = 1:length(n_vector)
if length(n_vector(i:end)) > 1
if n_vector(i) == n_vector(i+1)
n_vector(i+1) = [];
t_vector(i+1) = [];
u = 1;
else
u = 0;
end
elseif length(n_vector) == 1
u = 0;
else
break
end
end
end
Is there a way that I can fix this code so it can delete values no matter how many times it repeats, or do I need to do try something else all together?
Sincerely, Colin
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!