Matrix times array
Versión 1.0.0 (3,2 KB) por
Matthias Kredler
Fast multiplication of multi-dimensional array x by matrix A along dim. n (or: mode-n product of tensor x and matrix A)
out = MatrixTimesArray(A,x,i);
Multiplication of multi-dimensional array x by matrix A along dimension i. This is equivalent to mode-i product of tensor x by matrix A in tensor language, see for example De Lathauwer et al. (SIAM 2000). No further mex-files needed.
Elements of output
are determined by the formula:
The function reshapes x into a matrix (performing a permute, if necessary) and then either pre- or post-multiplies it by A, whichever minimizes permute operations). If x is a matrix, the following equivalence holds to conventional matrix algebra:
A*x = MatrixTimesArray(A,x,1); % dim. 1: matrix pre-multiplication
x*B'= MatrixTimesArray(B,x,2); % dim. 2: matrix post-multiplication
As for speed: For large arrays, note that the permute operations cost time time, not the reshape. Thus, put most computationally intense dimensions on first (preferred) and last dimension.
Inputs:
A:
matrix, full or sparse.
x: k-dimensional array of size ![](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAANkAAAAoCAYAAABzcF/KAAAHH0lEQVR4nO2d3YuVRRjAf6vrmuu6Zua6pqalYWquKBli2YdKIBmVq2CksqaZqWGalmaoFX0QWZTURSiFsYghEiGyLMSCCAuCF1544cVeebW35x84XTwzvLOz7/f7njnu7vzgRfY8M88885z5fGbeI3g8Ho/H4/F4PB6PZ6SwGRgAJtfbEI+nDnQCfcDcWhXwLlAF3qlVAR7PA04jcBUYBOaVrXwF0sE+L1uxxzPCaAZuqWdiWUqnIkvES0BDWUo9nhHMTGQ2O1eWwsvILFb69OjxjGDeQ/rF20UVbVaKfimqyOMZZUwE7iP9o7mIoutKyZwSjPJ4Rhv7KTibPaYU3CnLIo9nlLEQ6SO9eRUcUgq+K8sij2cUMoj0k/l5MveqzK+mTD8H2AicABYZn3cAR4AzeQ0pQDOwEtiFTO2aycBW4Gvk3G+cY7tcMhZ94LItXkD6yfY8mQdI7qHjgB6gotLqZxbQBHxrfV5BDvRqTSdw1ypbB29WEtRNPwcc2OSaseaDerXFY0rfiawZGwxjpiakXa4K0On7kQr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h9EKz1zVd+nrNsqgKPJuhZZ6V/yZKb/uqzZC590IjMalWGhrqz2JCmTnH+yOrfom3xCYZ25sXq7z3IhBOG9sfiGLsSmYP0/HuEz2Z6pLkekf8UQcVuEH0Wk9TJdJRLP3F7o11GutUh8mkM1dUVo0unrwBbrM+bGX7/LyqAoJlBsEyrMPxYwPTXEUvm2gdnlcwMdWexQRNXpzh/ZPVv0bbYRTALrkI64YqY8lar9LlC9zY6wpTrljFi8FLiQ6Jp9mTtyDlPFfg5py2aNsRJSSH5BuTeW9RSpQm5TaAvnyYdd4CMxhsIn9HHEfgrDJc+0EEE++f/stqQVKc4f+TxbxxxbbFblXEIqXvSz771qnSlnZ9+hYw0UY4qStrooo7+RG2iy+YksiSIO5BvQka9zHfXcuLCB3NVGVEvI7r8Hlz41wwE6Vk17gD+qEqXdLyViUbkwPg+8upA2aTpZPqLPVmD8sPYi+wh7BHWdH4rsmQaQNb0tcaVD3aocj6uow3gzr/6zYQ/COIQUb8v2qnku2thyDSk0d0meiOYl6ROtkDJXV1i3YZsju0N9DMMDeNfQPYALm6CuPSBPiNdjtyi0Ptx19+DK/8eRuq1kWAWv4MsdR8BnlTpVinZJ7U0ZgKwifLfotXXYSpEL81mR3xeNpsIOnyP8ejoVKeRdhZuD2td+eAOUtctyOheDxvAnX97kPpOUX/rQeY4EtjQRxULcLdVKRX9Qp0Zscpy4l4mTxt2hD1Ja/XRQh9S32uM/v/3oJEg4qhpI2iTYQf0Hk9hmoHH621EnWkh260Wj8fj8Xg8Ho/H4/F4PB6Px+Mpmf8B6p7eFIBj0yMAAAAASUVORK5CYII=)
i: Scalar, optional. Along which dimension to multiply. If omitted, the function multiplies along the first compatible dimension
Output:
Example:
A = rand(60,30); % Matrix A: 60-by-30.
x = rand(50,30,20); % Array/tensor x: (50,30,20)
y = MatrixTimesArray(A,x,2); % Multiply along dimension 2:
size(y) % y is (50,60,20) array.
yy= MatrixTimesArray(A,x); % Omitting last input (dim.) gives same result.
norm(yy(:)-y(:));
Version: 20-Dec-2021
Author: Matthias Kredler (Universidad Carlos III de Madrid), mkredler@eco.uc3m.es
Acknowledgement: This code follows the same idea as my function 'kronm.m', which carries out a series of MatrixTimesArray / n-mode products on a tensor.
Citar como
Matthias Kredler (2024). Matrix times array (https://www.mathworks.com/matlabcentral/fileexchange/103825-matrix-times-array), MATLAB Central File Exchange. Recuperado .
Compatibilidad con la versión de MATLAB
Se creó con
R2021b
Compatible con cualquier versión
Compatibilidad con las plataformas
Windows macOS LinuxEtiquetas
Agradecimientos
Inspirado por: Fast Kronecker matrix multiplication, Fast and Efficient Kronecker Multiplication
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Descubra Live Editor
Cree scripts con código, salida y texto formateado en un documento ejecutable.
Versión | Publicado | Notas de la versión | |
---|---|---|---|
1.0.0 |