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
Run the command by entering it in the MATLAB Command Window.
Get MATLAB
Get a Free 30-Day Trial Run MATLAB in your browser or download and install on your desktop.
🌐
MathWorks
mathworks.com › wavelet toolbox › filter banks › lifting
mtimes - Laurent polynomial or Laurent matrix multiplication - MATLAB
This MATLAB function returns the product of the pair of Laurent polynomials or Laurent matrices A and B.
🌐
MathWorks
mathworks.com › robotics system toolbox › coordinate transformations
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 › robotics system toolbox › coordinate transformations
mtimes, * - Quaternion multiplication - MATLAB
This MATLAB function implements quaternion multiplication if either A or B is a quaternion.
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 › linear algebra
pagemtimes - Page-wise matrix multiplication - MATLAB
This MATLAB function computes the matrix product of corresponding pages of the N-D arrays X and Y.
Find elsewhere
🌐
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 ...
🌐
MathWorks
mathworks.com › matlab › mathematics › elementary math › arithmetic operations
times - Multiplication - MATLAB
This MATLAB function multiplies arrays A and B by multiplying corresponding elements.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 77832-does-1-2-call-times-or-mtimes
Does 1.*2 call times or mtimes? - MATLAB Answers - MATLAB Central
June 3, 2013 - Well, all I can say is that I would hate for 1.*2 to call MTIMES when a.*2 calls TIMES. I'm glad operators are parsed with higher precedence than numbers. ... Sorry, while Matlab calls exactly the function I expect, it does not call the function names as I expect.
🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support
MTIMESX - Fast Matrix Multiply with Multi-Dimensional Support - File Exchange - MATLAB Central
February 23, 2011 - James Tursa (2026). MTIMESX - Fast Matrix Multiply with Multi-Dimensional Support (https://www.mathworks.com/matlabcentral/fileexchange/25977-mtimesx-fast-matrix-multiply-with-multi-dimensional-support), MATLAB Central File Exchange.
🌐
GitHub
github.com › JHvisionchen › SRDCF-matlab › blob › master › mtimesx.m
SRDCF-matlab/mtimesx.m at master · JHvisionchen/SRDCF-matlab · GitHub
% mtimesx does a matrix multiply of two inputs (single, double, or sparse) %****************************************************************************** % · % MATLAB (R) is a trademark of The Mathworks (R) Corporation · % · % Function: ...
Author: JHvisionchen
Top answer
1 of 1
7

You just need to cast the data to double, then run your code again. Basically what the error is saying is that you are trying to mix classes of data together when applying a matrix multiplication between two variable. Specifically, the numerical vectors and matrices you define in dft1 are all of a double type, yet your image is probably of type uint8 when you read this in through imread. This is why you're getting that integer error because uint8 is an integer class and you are trying to perform matrix multiplication with this data type with those of a double data type. Bear in mind that you can mix data types, so long as one number is a single number / scalar. This is also what the error is alluding to. Matrix multiplication of varaibles that are not floating point (double, single) is not supported in MATLAB so you need to make sure that your image data and your DFT matrices are the same type before applying your algorithm.

As such, simply do:

I = imread('sample.jpg');
R = dft1(double(I));

Minor Note

This code is quite clever, and it (by default) applies the 1D DFT to all columns of your image. The output will be a matrix of the same size as I where each column is the 1D DFT result of each column from I.

This is something to think about, but should you want to apply this to all rows of your image, you would simply transpose I before it goes into dft1 so that the rows become columns and you can operate on these new "columns". Once you're done, you simply have to transpose the result back so that you'll get your output from dft1 shaped such that the results are applied on a per row basis. Therefore:

I = imread('sample.jpg');
R = dft1(double(I.')).';

Hope this helps! Good luck!

🌐
MathWorks
mathworks.com › matlabcentral › fileexchange › 82099-mtimesm-an-efficient-nd-matrix-multiplication-routine
mtimesm, an efficient nD matrix multiplication routine. - File Exchange - MATLAB Central
June 30, 2022 - MTIMESX (`PAGEMTIMES`) is a very efficient matrix multiply routine for n-Dimensional (nD, n > 2) arrays. However, you have to update your MATLAB (`PAGEMTIMES` was introduced in R2020b), or build a mex-file.
🌐
MathWorks
mathworks.com › matlabcentral › answers › 70853-times-sign-in-matlab
times sign in matlab - MATLAB Answers - MATLAB Central
April 7, 2013 - Hi, I am looking for a sign with I can use to write the times-sing (*), but in the midddle. I used this: text(8.3,1,'\delta \times Stock','color','k'); unfortunately \times gives me just ...