In this simple case, you could get by with using

sum(x.*x)

It seems times (.*) is supported properly for integer matrices, although mtimes ( *) is not.

For general matrix multiplication: let A and B be two matrices with suitable sizes so that A*B exists. Since times and sum are supported for integers, you can generalize the above trick, usingbsxfun and sum to compute all entries of the product matrix as follows.

Edit: As noted by @July, you need the 'native' flag in sum in order to keep the result of integer type. Thanks also for pointing out a problem that was caused by squeeze, now corrected by using a second permute.

permute(sum(bsxfun(@times, A.', permute(B, [1 3 2])), 1, 'native'), [2 3 1])

For example:

>> A = int64([1 2; 3 4])
A =
                    1                    2
                    3                    4
>> B = int64([5 7 9; 6 8 10])
B =
                    5                    7                    9
                    6                    8                   10
>> permute(sum(bsxfun(@times, A.', permute(B, [1 3 2])), 'native'), [2 3 1])
ans =
    17    23    29
    39    53    67

Anyway, the fastest alternative seems to be double(A)*double(B).

Answer from Luis Mendo on Stack Overflow
Contact MathWorks Support
Contact MathWorks Support or browse support resources
MATLAB Help Center
Documentation, examples, videos, and other support resources for MathWorks products including MATLAB and Simulink.
Documentation Home
Help CenterHelp Center · You clicked a link that corresponds to this MATLAB command:
Get MATLAB
Get a Free 30-Day Trial Run MATLAB in your browser or download and install on your desktop.
🌐
MathWorks
mathworks.com › navigation toolbox › coordinate transformations and trajectories
mtimes, * - Transformation or rotation multiplication - MATLAB
This MATLAB function performs transformation multiplication between transformation transformationA and transformation transformationB and returns the product, transformation transformationC.
🌐
MathWorks
mathworks.com › wavelet toolbox › filter banks › lifting
mtimes - Laurent polynomial or Laurent matrix multiplication - MATLAB
Q = mtimes(A,B) returns the product of the pair of Laurent polynomials or Laurent matrices A and B.
🌐
MathWorks
mathworks.com › econometrics toolbox › data preprocessing
LagOp.mtimes - Lag operator polynomial multiplication - MATLAB
Given two lag operator polynomials A(L) and B(L),C = mtimes(A, B, 'Tolerance',tolerance) performs a polynomial multiplication C(L) = A(L) * B(L). If at least one of A or B is a lag operator polynomial object, the other can be a cell array of matrices (initial lag operator coefficients), or ...
🌐
EDUCBA
educba.com › home › data science › data science tutorials › matlab tutorial › matrix multiplication in matlab
Matrix Multiplication in Matlab | How to Perform Matrix Multiplication?
March 24, 2023 - Initially, Matlab designed for the implementation of matrix operations. By using Matlab we can easily implement complex operations ad problems very easily. As we know in matrix operations multiplication is one of the difficult and complicated operations but by using simple command ‘mtimes’ we multiply two matrices.
Address: Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
Find elsewhere
Top answer
1 of 2
5

In this simple case, you could get by with using

sum(x.*x)

It seems times (.*) is supported properly for integer matrices, although mtimes ( *) is not.

For general matrix multiplication: let A and B be two matrices with suitable sizes so that A*B exists. Since times and sum are supported for integers, you can generalize the above trick, usingbsxfun and sum to compute all entries of the product matrix as follows.

Edit: As noted by @July, you need the 'native' flag in sum in order to keep the result of integer type. Thanks also for pointing out a problem that was caused by squeeze, now corrected by using a second permute.

permute(sum(bsxfun(@times, A.', permute(B, [1 3 2])), 1, 'native'), [2 3 1])

For example:

>> A = int64([1 2; 3 4])
A =
                    1                    2
                    3                    4
>> B = int64([5 7 9; 6 8 10])
B =
                    5                    7                    9
                    6                    8                   10
>> permute(sum(bsxfun(@times, A.', permute(B, [1 3 2])), 'native'), [2 3 1])
ans =
    17    23    29
    39    53    67

Anyway, the fastest alternative seems to be double(A)*double(B).

2 of 2
1
  • MATLAB does not support integer matrix-matrix multiplication due to the lack of industrial support.

    MATLAB uses BLAS (more specifically, Intel's implementation of BLAS, i.e. MKL) to do matrix multiplication. At present, BLAS does not natively support multiplication of integer matrices. However, a good news is that MKL 2018 adds initial support for integer matrices. (cf. this slides)

  • As for your question, if you aim at performance and your matrices are not too small, sum(x.*x) is inferior to int64(double(x)*double(x.')).

  • Use native only when you are sure that overflow and underflow will not happen.

    One extra note is that although native flag can keep the type of return value the same as that of input value. It may suffer from integer overflow or underflow. Check with the following snippet.

    sum(int8(-ones(300,1)), 'native') % ans = -128 (of type int8)
    sum(int8(ones(300,1)), 'native')  % ans = 127  (of type int8)
    sum(int8(ones(300,1)))            % ans = 300  (of type double)
    

    Although single and double can also overflows and underflows, they happens less frequently.

🌐
MathWorks
mathworks.com › matlab › mathematics › elementary math › arithmetic operations
times - Multiplication - MATLAB
This MATLAB function multiplies arrays A and B by multiplying corresponding elements.
🌐
Matlab Scripts
matlabscripts.com › matlab-multiplication
Mastering Matlab Multiplication: A Quick Guide
July 2, 2025 - `mtimes` Function The `mtimes` function is equivalent to the `*` operator and is used to perform matrix multiplication. This function can be particularly handy in more complex calculations or when dealing with function handles.
🌐
Matlab Scripts
matlabscripts.com › multiplication-matlab
Mastering Multiplication in Matlab: A Quick Guide
July 13, 2025 - This operator allows you to multiply two matrices or arrays element by element. Here’s an example: % Element-wise Multiplication Example A = [1, 2; 3, 4]; B = [5, 6; 7, 8]; result = A .* B; % result will be [5, 12; 21, 32] In this code, each corresponding element of matrices `A` and `B` is multiplied together, resulting in a new matrix. The `mtimes` function offers an alternative method for performing matrix multiplication...
🌐
MathWorks
mathworks.com › matlab › mathematics › linear algebra
pagemtimes - Page-wise matrix multiplication - MATLAB
Each dimension with size 1 (after the first two dimensions) is implicitly expanded to match the dimension size of the other input, and then each page of the output Z(:,:,i,j) is formed by multiplying X(:,:,i,j)*Y(:,:,i,j). An intuitive way to think about this operation is that X contains two matrices as pages in a 3-D array, and Y contains four matrices arranged along the fourth dimension; thus, multiplying all combinations of these matrices results in eight 3-by-3 matrices. ... Input arrays, specified as dense matrices or multidimensional arrays. The pages of X and Y must be valid inputs to a matrix product (mtimes, *).