Please suggest a method using Vectorization.

1 visualización (últimos 30 días)
Vinod S
Vinod S el 13 de Oct. de 2012
I want to do the following operation using vectorization technique of Matlab. I am a novice user of matlab. So please tell me how i can get the following operation done without using the FOR Loop.
x=[1 2 3 4 5 6 7 8 9 10 11 1 2];
for i=1:length(x)
x(i)=((-1)^i)*x(i);
end

Respuesta aceptada

Matt Fig
Matt Fig el 13 de Oct. de 2012
Editada: Matt Fig el 13 de Oct. de 2012
Many ways to do it.
Here is one:
x = x.*(-1).^(1:length(x))
and here is another:
x(1:2:end) = -x(1:2:end)

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by