How to replace every other element in an array with a different number
5 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
EA
el 5 de Sept. de 2020
I have a vector that I am trying to turn every other entry in the array to be negative.
So it would look like x = [200 -198 196 -194 ... -2 0]
This is what I have. Is there a different approach I should be taking?
x = [200:-2:0]
x(2:2:end) = ()
1 comentario
Stephen23
el 6 de Sept. de 2020
Editada: Stephen23
el 6 de Sept. de 2020
"I have a vector that I am trying to turn every other entry in the array to be negative."
What should happen if a value is already negative? Is the intent to simply negate values regardless of their signs, or to actually negate absolute values (which matches what you described). So far it is not clear.
Respuesta aceptada
Más respuestas (1)
David Hill
el 5 de Sept. de 2020
a=yourVector.*(-ones(size(yourVector))).^(0:length(yourVector)-1);
0 comentarios
Ver también
Categorías
Más información sobre Cell Arrays en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!