Multiplying a 2d matrix with each slice of 3d matrix

34 次查看(过去 30 天)
What is the fastest way multiply a 2d matrix with each slice of a 3d matrix?
x = rand(1000,200);
F = rand(100,1000,10);
b = zeros(100,200,10);
for i = 1:10
b(:,:,i) = F(:,:,i)*x;
end
  2 个评论
Shravankumar P
Shravankumar P 2014-8-2
I would like to have the transposed F. i.e., F=F'; is it possible.
Matt J
Matt J 2014-8-4
@Shravankumar
MTIMESX has input flags that let you say whether you want the multiplication with F, F', or F.'

请先登录,再进行评论。

采纳的回答

Andrei Bobrov
Andrei Bobrov 2014-8-2
f1 = size(F);
x1 = size(x);
b = reshape(sum(bsxfun(@times,reshape(F,[f1(1),1,f1(2:3)]),...
reshape(x',1,x1(2),[])),3),f1(1),x1(2),[]);
  2 个评论
Ray M
Ray M 2021-10-11
Great Solution, the solution you have provided is equivalent to:
for i = 1:whatever
b(:,:,i) = F(:,:,i) * x;
end
How would you produce the trasnpose multiplication effect (assuming correct sizes)?
for i = 1:whatever
b(:,:,i) = x * F(:,:,i);
end

请先登录,再进行评论。

更多回答(4 个)

Steven Lord
Steven Lord 2021-10-11
In release R2020b we introduced the pagemtimes function for this purpose.
  3 个评论
Ray M
Ray M 2021-10-11
编辑:Ray M 2021-10-11
I am not sure if this meant to be a reply to my question under Andrei Bobrov solution, but it does not appear so. I am simply interested in performing a single matrix multiplication by slices of a 3D matrix with the use of bsxfun as follows:
mat_mxn * 3Dmat_nxkxl
and
3Dmat_mxnxk * mat_nxh
1) We note that A * B ~= B * A. Note that Andrei Bobrov solution just does 3Dmat_mxnxk * mat_mxh.
2) @times in bsxfun perform .* not * and that is why Andrei Bobrov does a sum as well.
Thanks
Steven Lord
Steven Lord 2021-10-12
@Image Analyst Your bsxfun call is calling times not mtimes. times can work with implicit expansion.
A = int16(magic(4));
B = repmat(A, 1, 1, 3);
C = A.*B
C = 4×4×3 int16 array
C(:,:,1) = 256 4 9 169 25 121 100 64 81 49 36 144 16 196 225 1 C(:,:,2) = 256 4 9 169 25 121 100 64 81 49 36 144 16 196 225 1 C(:,:,3) = 256 4 9 169 25 121 100 64 81 49 36 144 16 196 225 1
mtimes isn't fully defined for integer arrays. If mtimes doesn't work pagemtimes probably shouldn't.
D = A*A % errors
Error using *
MTIMES (*) is not fully supported for integer classes. At least one argument must be scalar.

请先登录,再进行评论。


Edric Ellis
Edric Ellis 2014-8-4
With Parallel Computing Toolbox, you can perform this on the GPU using PAGEFUN.
  1 个评论
Nils Melchert
Nils Melchert 2020-5-19
编辑:Nils Melchert 2020-5-19
Is there the possibility to get a minimal example for exactly this use case? I am struggling with the same thing.

请先登录,再进行评论。


Matt J
Matt J 2014-8-2
编辑:Matt J 2014-8-2
See MTIMESX.
  9 个评论
Matt J
Matt J 2014-8-2
编辑:Matt J 2014-8-2
Do you have any experience installing MTIMESX on os x
Don't see why it would be different than Windows, except I'm not sure that SPEEDOMP would be supported. What happens when you try to compile with mtimesx_build?
Søren sønderby
Søren sønderby 2014-8-2
It fails with the following output:
Non-PC auto build is not currently supported. You will have to
manually compile the mex routine. E.g., as follows:
>> blas_lib = 'the_actual_path_and_name_of_your_systems_BLAS_library'
>> mex('-DDEFINEUNIX','mtimesx.c',blas_lib)
or
>> mex('-DDEFINEUNIX','-largeArrayDims','mtimesx.c',blas_lib)
I tried to google the blas library path on osx 10.7.5 with no luck so far.

请先登录,再进行评论。


Image Analyst
Image Analyst 2014-8-2
You can do it easily if the number of rows and columns in your 3D and 2D match, which they don't in your example:
rows = 1000;
columns = 200;
slices = 10;
x = rand(rows, columns);
F = rand(rows, columns, slices);
b = zeros(rows, columns, slices);
for slice = 1 : slices
b(:,:, slice) = F(:,:, slice) .* x; % Use dot star, not just star.
end
If the number of rows and columns are different you need to make some decisions about exactly where you want to multiply, if one is smaller than the other, or one extends out past the other.
  1 个评论
Image Analyst
Image Analyst 2014-8-2
Alternate way. Not sure which is faster:
% Mask the 3D image called "image3D" with 2D image called "mask".
masked3DImage = bsxfun(@times, image3D, cast(mask, class(image3D)));

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 GPU Computing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by