Calculate difference between successive vector values.

Hi all, Hopefully a straightforward question, but one that I'm struggling with. I have a vector and want to create a new vector whose values are equal to the difference between successive values from the previous vector. e.g A = [1,2,3,3,0] want B = [1,1,0,-3]
What is the simplest way of doing this? Many thanks in advance. Will

 채택된 답변

Azzi Abdelmalek
Azzi Abdelmalek 2012년 8월 21일
편집: Azzi Abdelmalek 2012년 8월 21일

19 개 추천

A=[1,2,3,3,0]
B=diff(A)

댓글 수: 5

Jan
Jan 2012년 8월 21일
편집: Jan 2012년 8월 21일
This is much simpler and faster than A(2:end) - A(1:end-1). +1
Brilliant, thank you!
Is there any way to solve this problem using colon operator? Thanks!
The method given in Jan's comment above uses the colon operator:
A = [1,2,3,3,0];
B = A(2:end) - A(1:end-1)
Was looking for a function that did this, thanks!!!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

질문:

2012년 8월 21일

댓글:

Dav
2023년 8월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by