Matrix multiplication of 3d arrays
70 views (last 30 days)
I have a 3d array of dimension 2*5000*150 and another array of dimension 5000*1*150. I would like to take each 2*5000 matrix in the first 3d array and multiply it with the corresponding 5000*1 matrix in the second 3d array.
I need to do this in as efficient a way as possible and without any additional functions that are not preinstalled on a standard matlab installation.
Ameer Hamza on 16 May 2018
Just use for loop
result = zeros(2,1,150);
result(:,:,i) = matrix1(:,:,i)*matrix2(:,:,i);
James Tursa on 16 May 2018
Edited: James Tursa on 16 May 2018
Some options from the FEX:
MTIMESX: (C-mex code requiring C compiler) needs updated build routine for later MATLAB versions
MMX: (C-mex code requiring C compiler)
And raw times code:
result = sum(bsxfun(@times,matrix1,reshape(matrix2,1,5000,150)),2);
result = sum(matrix1.*reshape(matrix2,1,5000,150),2); % later versions of MATLAB