Change values in vectors

I have a vector like: V=[1,2,3,4,5,6] and I want to substitute any values above 3 with the previous value in the vector which is “2”. So I want to have V=[1,2,2,2,2,2]. How to do this? Thank you.

Respuestas (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 21 de En. de 2023
Editada: Sulaymon Eshkabilov el 21 de En. de 2023
V=[1,2,3,4,5,6]
V = 1×6
1 2 3 4 5 6
IDX = find(V>3);
V(IDX)=2
V = 1×6
1 2 3 2 2 2
%% ALT. Way:
V=[1,2,3,4,5,6]
V = 1×6
1 2 3 4 5 6
V(V>3)=2
V = 1×6
1 2 3 2 2 2
Vilém Frynta
Vilém Frynta el 21 de En. de 2023

0 votos

If you want to apply it to this vector, it's not complicated. But if you wanted to apply it to a more complex vector where the numbers don't go in ascending order, it woudle be bit more complicated.
I almost did it for you, but then I realized this sounds like a homework.
Please try, then we may help you. Try using find and indexing.

1 comentario

Vilém Frynta
Vilém Frynta el 21 de En. de 2023
Well, nevermind, someone did it for you ÷)

Iniciar sesión para comentar.

Categorías

Más información sobre Programming en Centro de ayuda y File Exchange.

Productos

Versión

R2022b

Etiquetas

Preguntada:

Io7
el 21 de En. de 2023

Comentada:

el 21 de En. de 2023

Community Treasure Hunt

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

Start Hunting!

Translated by