??? Error using ==> mtimes Integer data types are not fully supported for this operation. At least one operand must be a scalar.

4 views (last 30 days)
I am facing this error when I'm multiplying a matrix (40000*40) by its transpose matrix. MIt=MI';

Accepted Answer

James Tursa
James Tursa on 12 Oct 2015
Generic matrix multiplication for integer data types is not supported by MATLAB. You will need to convert them to a floating point type (double or single) first if you want to do this operation. E.g.,
dMI = double(MI);
result = dMI * dMI';
  2 Comments
James Tursa
James Tursa on 12 Oct 2015
If you are asking why your matrix is an integer data type, then I can't answer that ... you will need to look at your code to determine this.
If you are asking why generic matrix multiplication is not supported for integer data types, it is likely because such an operation result is ambiguous due to the truncating nature of integer arithmetic in MATLAB. E.g., take this simple dot product example:
>> x = int8([100 100 -100])
x =
100 100 -100
>> y = int8([1 1 1])
y =
1 1 1
>> y*x'
Error using *
MTIMES is not fully supported for integer classes. At least one input must be scalar.
To compute elementwise TIMES, use TIMES (.*) instead.
>> double(y)*double(x)'
ans =
100
>> y(1)*x(1) + y(2)*x(2) + y(3)*x(3)
ans =
27
>> (y(1)*x(1) + y(2)*x(2)) + y(3)*x(3)
ans =
27
>> y(1)*x(1) + (y(2)*x(2) + y(3)*x(3))
ans =
100
The dot product result should be this, right?
1*100 + 1*100 - 1*100 = 100
The straight multiply fails with an error as you have seen. And converting to double first gives the expected answer. But doing the calculation manually one term at a time yields a different answer. What is going on? It is because integer arithmetic truncates, so depending on how one groups the intermediate results one can get different answers. The first intermediate sum is 200 which is larger than the largest int8 value of 127, so it gets truncated to 127. Then the next operation subtracts 100 so you get the final answer of 27. But grouping the last two terms together yields the 100 answer. It is because of this ambiguity that MATLAB does not support this operation.

Sign in to comment.

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!