working with a vector

조회 수: 14 (최근 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에서 Programming에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by