working with a vector

1 次查看(过去 30 天)
Federico Paolucci
Federico Paolucci 2022-12-17
评论: Voss 2022-12-17
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

采纳的回答

Voss
Voss 2022-12-17
% 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 个评论
Federico Paolucci
Federico Paolucci 2022-12-17
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 2022-12-17
You're welcome!
x = [1;4;5;7;10];
new_x = [x(1); diff(x)]
new_x = 5×1
1 3 1 2 3

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by