Subtracting the next element on a vector? Basic

7 Ansichten (letzte 30 Tage)
Renato de Sá
Renato de Sá am 9 Feb. 2011
Kommentiert: Steven Lord am 14 Jan. 2021
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.

Akzeptierte Antwort

Jiro Doke
Jiro Doke am 9 Feb. 2011
A = [20, 8, 4, 1];
B = -diff(A);
  2 Kommentare
Renato de Sá
Renato de Sá am 9 Feb. 2011
Thank you, sir.
Steven Lord
Steven Lord am 14 Jan. 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Environment and Settings finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by