Efficient Summation of two matrices multiplied

2 次查看(过去 30 天)
Hello,
I have a summation that I have been trying to vectorize using block processing, but have been unable to find an underlying structure to compute this. The equation is:
I understand that this can be implemented using for loops and conditional statements, however, this is very slow and doesn't work efficiently as the matrices get larger.
It's also fine to loop through α and β as the B matrix will typically not get so big to where it would bog down a double for loop. However, if it can be fully vectorized including α and β that would be even better.
Thank you in advance!
Alex
EDIT:
As was pointed out, the equation can likely instead be solved for:
which allows for the appropriate indexing of the "4D" matrix.
Here is some code that works:
A = randn(8,8)+1i*randn(8,8);
data = randn(16,16) + 1i*randn(16,16);
presums = size(A);
A = repmat(A,8,8);
out = foo(data,A,idx);
function out = foo(data,A,idxd)
k = 0:idxs(1)-1;
l = k;
p = 0:idxs(2)-2;
q = p;
t = (idxs(1)):size(data,1)-1;
s = (idxs(2)):size(data,2)-1;
a = 1:size(data,1);
b = 1:size(data,2);
out = zeros(size(data));
for aa = a
for bb = b
for tt = t
for kk = k
if tt - kk == aa
for ss = s
for pp = p
if ss - pp == bb
for qq = q
for ll = l
out(aa,bb) = out(aa,bb) + A(kk+idxs(1)*(pp)+1,ll+idxs(1)*(qq)+1)*...
data(tt-ll+1,ss-qq+1);
end
end
end
end
end
end
end
end
end
end
end
  2 个评论
Walter Roberson
Walter Roberson 2023-8-29
are you sure k=0..m and not k=0..m-1?
If it were m-1 then the equations would correspond to simulating a 4d matrix in a 2d matrix but with the way it is now, it is overlapping on the boundaries
Alex Batts
Alex Batts 2023-8-29

It’s very possible that is the case. The equation as is is the way I derived it. However, A is supposed to be a block diagonal matrix which could be represented as a 4d matrix. Would that then also make t=[m … M-1]?

请先登录,再进行评论。

回答(1 个)

Sugandhi
Sugandhi 2023-9-21
Hi,
I understand that you are trying to perform a sum over a 4D array, where each element of the array is a 2x2 matrix. You want to sum up all the elements of the 4D array, but only when certain conditions are met.
The equation you provided is correct, but there might be a mistake in the bounds of the loop. If the dimensions of the 4D array are M x N x P x Q, then the loops should go from 0 to M-1, 0 to N-1, 0 to P-1, and 0 to Q-1.
To avoid looping over the entire 4D array, you can use advanced indexing to extract the desired subarray and then sum it up.

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by