I have a 4-D array with dimensions A = A1 x A2 x A3 x N and a vector with dimensions V = 1 x N. I would like to multiply each element in the array A with vector V along fourth dimension and take a sum along the same dimension to form a matrix B = A1 x A2 x A3. Currently I am using a loop to achieve this, but is there any faster method to achieve the same result? Thanks!
A1 = 3;A2 = 3;A3 = 3;N = 10;A = ones(A1, A2, A3, N);V = ones(1, N);B = NaN(A1, A2, A3);for i = 1:A1for j = 1:A2for k = 1:A3 B(i,j,k) = squeeze(A(i,j,k,:))' * V';endendend
Best Answer