Product of elementwise differences

조회 수: 2 (최근 30일)
Chethan S
Chethan S 2011년 5월 8일
As a part of a large function in my MATLAB code I have to calculate difference between elements of matrices and sum them up in a special way. Let me illustrate my case with sample matrices:
1 2 3 4
9 10 11 12
and
5 6 7 8
13 14 15 16
My calculations goes as follows:(Zeroes appear due to padding)
(1-2)(5-6)+(2-3)(6-7)+(3-4)(7-8)+(4-0)(8-0): This replaces 1 in initial matrix.
(2-3)(6-7)+(3-4)(7-8)+(4-0)(8-0)+(0-0)(0-0): This replaces 2 in initial matrix.
(3-4)(7-8)+(4-0)(8-0)+(0-0)(0-0)+(0-0)(0-0): This replaces 3 in initial matrix.
........
Coming to second row of matrices:
(9-10)(13-14)+(10-11)(14-15)+(11-12)(15-16)+(12-0)(16-0): This replaces 9 in initial matrix.
(10-11)(14-15)+(11-12)(15-16)+(12-0)(16-0)+(0-0)(0-0): This replaces 10 in initial matrix.
.........
As an inexperienced programmer I am finding this task really hard. Can someone please help me?

채택된 답변

Walter Roberson
Walter Roberson 2011년 5월 8일
Not much too this one.
fliplr(cumsum(diff([zeros(size(A,1)),fliplr(A)],1,2) .* diff([zeros(size(B,1)),fliplr(B)],1,2),2))

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Resizing and Reshaping Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by