Subtraction of elements in successive rows

조회 수: 9 (최근 30일)
Shreya George
Shreya George 2021년 2월 1일
댓글: Shreya George 2021년 2월 2일
I have a matrix 'a'. I need an output matrix 'A' with each row equal to the difference between successive rows of the parent matrix 'a'. Is there a way to do this? I tried coding it in the following manner but keep getting an error message 'Unable to perform assignment because the left and right sides have a different number of elements.'
a=[1 2;2 4;3 6];
A=zeros(2,2);
for i=1:2
A(i)=(a(i+1,:)-a(i,:));
end
The expected result is A=[1 2; 1 2]

채택된 답변

William
William 2021년 2월 1일
The easy way to do this is
A = diff(a);
  댓글 수: 2
Stephen23
Stephen23 2021년 2월 1일
Even better, specify the dimension:
b = diff(a,1,1)
ans = 2×2
1 2 1 2
Shreya George
Shreya George 2021년 2월 2일
Thanks

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Elementary Math에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by