user3024655
user3024655

Reputation:

Vectorization of a Sum over 3D array Matlab

I'd like to know how to vectorize in MATLAB this code so as to avoid the use of loops :

for i=1:n1
for j=1:n2
A(i,j) = sum(B(:,i,j).*C(:,i,j));
end
end

where A are a matrix of size n1*n2 and B,C are 3D arrays.

Thank you.

Upvotes: 0

Views: 44

Answers (1)

mpaskov
mpaskov

Reputation: 1264

You should be able to do it directly

A = sum(B.*C,1);

Or to remove the first dimension:

A = squeeze(sum(B.*C,1));

Upvotes: 3

Related Questions