Subtracting the next element on a vector? Basic

If I have a vector with n elements, and want a new vector with the subtraction of the first by the second element, and continues...
For exemple: A = [20, 8, 4, 1] turns into B = [(20-8),(8-4),(4,1)]
I started something with
n = length(A);
for k = 1:n
....
but nothing I tried worked them.
Thanks.

 채택된 답변

Jiro Doke
Jiro Doke 2011년 2월 9일

6 개 추천

A = [20, 8, 4, 1];
B = -diff(A);

댓글 수: 2

Thank you, sir.
Jayaram ram flagged this as "its working good sir...."
The intended purpose of flags is to alert the editors and/or the admins about spam or other content that's not appropriate for Answers. To comment on the solution please use a comment like I'm doing here. Thanks.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Environment and Settings에 대해 자세히 알아보기

제품

질문:

2011년 2월 9일

댓글:

2021년 1월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by