Product of elementwise differences

2 次查看(过去 30 天)
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