Product of elementwise differences

2 views (last 30 days)
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?

Accepted Answer

Walter Roberson
Walter Roberson on 8 May 2011
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))

More Answers (0)

Categories

Find more on Resizing and Reshaping Matrices in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!