MATLAB Answers

Consecutive subtraction of the entries of a vector

조회 수: 20(최근 30일)
salam mahama
salam mahama 13 Jan 2021
댓글: James Tursa 14 Jan 2021
I am a novist in MATLAB. I have no idea of how to subtract the entries of a vector consecutively. Say if A=[3,5,9,11,15]' and I want something like this B =[3, 2, 4, 2, 4]. Any help will do, thanks in advance.

  댓글 수: 0

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

채택된 답변

James Tursa
James Tursa 13 Jan 2021
편집: James Tursa 13 Jan 2021
If A is a row vector:
B = [A(1) diff(A)];
If A is a column vector:
B = [A(1);diff(A)];

  댓글 수: 3

salam mahama
salam mahama 14 Jan 2021
Thank you James. Based on your code, I tried find the division version but it seems matlab has no build in function for division. I would like to divide the consecutive entries of A to get something like this: A=[-, 5/3, 9/5, 11/9 ,15/9]' and I tried something like B = [A(1); A.\ A ] and that did not work. I would appreciate it if you can help. Thank you in advance.
salam mahama
salam mahama 14 Jan 2021
A typo B = [A(1); A./ A]
James Tursa
James Tursa 14 Jan 2021
B = [A(1);A(2:end)./A(1:end-1)];

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

추가 답변(0개)

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by