Multiplying a 2d matrix with each slice of 3d matrix
Mostrar comentarios más antiguos
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 comentarios
Shravankumar P
el 2 de Ag. de 2014
I would like to have the transposed F. i.e., F=F'; is it possible.
Respuesta aceptada
Más respuestas (4)
Steven Lord
el 11 de Oct. de 2021
2 votos
In release R2020b we introduced the pagemtimes function for this purpose.
3 comentarios
Image Analyst
el 11 de Oct. de 2021
Too bad it doesn't work with uint8 images (yet)
% Works
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, 'like', rgbImage));
% Doesn't work
maskedRgbImage = pagemtimes(rgbImage, cast(mask, 'like', rgbImage)); % R2020b and later.
Error using pagemtimes
Invalid data types. Input arrays must be double or single.
Error in test (line 10)
maskedRgbImage = pagemtimes(rgbImage, cast(mask, 'like', rgbImage)); % R2020b and later.
It would be great for using a logical 2-D mask image and a 2-D gray scale, or 3-D color, or N-d volumetric image. Can you put in that suggestion?
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
Thanks
@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
mtimes isn't fully defined for integer arrays. If mtimes doesn't work pagemtimes probably shouldn't.
D = A*A % errors
Edric Ellis
el 4 de Ag. de 2014
1 voto
1 comentario
Nils Melchert
el 19 de Mayo de 2020
Editada: Nils Melchert
el 19 de Mayo de 2020
Is there the possibility to get a minimal example for exactly this use case? I am struggling with the same thing.
9 comentarios
Matt J
el 2 de Ag. de 2014
Søren sønderby Commented:
I briefly looked at MTIMESX but I didn't find any installation instructions for OSX.
How would you do it using GPU?
Image Analyst
el 2 de Ag. de 2014
To use GPU computing you also need the Parallel Computing Toolbox. Have you purchased that?
Søren sønderby
el 2 de Ag. de 2014
Image Analyst
el 2 de Ag. de 2014
The Mathworks does not make any toolbox by the name "GPU Toolbox". If you have one, it must have been written by some third party company.
Søren sønderby
el 2 de Ag. de 2014
Søren sønderby
el 2 de Ag. de 2014
Image Analyst
el 2 de Ag. de 2014
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 comentario
Image Analyst
el 2 de Ag. de 2014
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)));
Categorías
Más información sobre Sparse Matrices en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!