Borrar filtros
Borrar filtros

working with a vector

1 visualización (últimos 30 días)
Federico Paolucci
Federico Paolucci el 17 de Dic. de 2022
Comentada: Voss el 17 de Dic. de 2022
Hi, I'm dealing with a column vector with elements growing from top to bottom: I would like each element following the first to be subtracted from its previous one. Thanks for your suggestions

Respuesta aceptada

Voss
Voss el 17 de Dic. de 2022
% a column vector:
x = [1;4;5;7;10]
x = 5×1
1 4 5 7 10
% one way:
diff(x)
ans = 4×1
3 1 2 3
% another way:
x(2:end)-x(1:end-1)
ans = 4×1
3 1 2 3
  2 comentarios
Federico Paolucci
Federico Paolucci el 17 de Dic. de 2022
Thank you very much, i have another question: is there a way to maintain "1" as the first element of the new vector? in this case, I would obtain [1;3;1;2;3] from x = [1;4;5;7;10]
Voss
Voss el 17 de Dic. de 2022
You're welcome!
x = [1;4;5;7;10];
new_x = [x(1); diff(x)]
new_x = 5×1
1 3 1 2 3

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre MATLAB en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by