Matrix multiplication bugged?

6 views (last 30 days)
Antonio Olinto
Antonio Olinto on 17 Jul 2025
Commented: Steven Lord on 17 Jul 2025
Matrix multiplication not working properly. It should be [0 1 0], right?
>> R
R =
0.0000 1.0000 0
0 0.0000 1.0000
1.0000 0 0.0000
>> R*([1 0 0].')
ans =
-0.0000
0
1.0000
  2 Comments
Umar
Umar on 17 Jul 2025
Edited: Umar on 17 Jul 2025

Hi @Antonio Olinto ,

Matrix multiplication in MATLAB can sometimes lead to confusion, especially when dealing with dimensions and orientations of matrices and vectors. Let's break down the problem step by step to clarify why the output is not as expected.

Matrix ( R ): The matrix ( R ) is defined as follows:

  R =
 0.0000    1.0000         0
 0.0000    0.0000    1.0000
 1.0000         0   0.0000


This is a ( 3 times 3 ) matrix. The column vector you are multiplying with is:
[1 0 0]’ is a ( 3 times 1 ) column vector, is the transpose of the row vector ([1, 0, 0]). For matrix multiplication to be valid, the number of columns in the first matrix must equal the number of rows in the second matrix. In this case:

  • ( R ) is ( 3 times 3 )
  • The column vector is ( 3 times 1 )

Since the dimensions align (3 columns in ( R ) and 3 rows in the column vector), the multiplication is valid.

The multiplication ( R * ([1 0 0].') ) can be computed as follows:

First Row: [ 0 cdot 1 + 1 cdot 0 + 0 cdot 0 = 0 ] Second Row: [ 0 cdot 1 + 0 cdot 0 + 1 cdot 0 = 0 ] Third Row: [ 1 cdot 1 + 0 cdot 0 + 0 cdot 0 = 1 ]

Thus, the resulting vector is:

ans =
   0
   0
   1

So, the expected result of ([0, 1, 0]) suggests that there may have been a misunderstanding of the operation being performed. The multiplication of ( R ) with the column vector ([1, 0, 0]) does not yield ([0, 1, 0]) because the operation is fundamentally different from what was anticipated. So, in nutshell, the output you received, which is:

ans =
   0
   0
   1

is indeed correct based on the rules of matrix multiplication. If you were expecting a different result, it may be beneficial to revisit the mathematical operations you intended to perform. If you have further questions or need clarification on specific aspects of matrix operations in MATLAB, feel free to ask!

Hope this helps.

Stephen23
Stephen23 on 17 Jul 2025
Edited: Stephen23 on 17 Jul 2025
"It should be [0 1 0], right?"
No, that would not follow the standard rules of linear algebra. Please explain why you think that should be the output.
A = [0,1,0;0,0,1;1,0,0]
A = 3×3
0 1 0 0 0 1 1 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B = [1;0;0]
B = 3×1
1 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A*B
ans = 3×1
0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
We can even perform a simple sanity check: given two matrices A & B with sizes PxQ and QxR respectively then when multiplied together A*B we know that the output must have size PxR. In this case 3x3 and 3x1 must give 3x1, which matches exactly what MATLAB returns.
In contrast your proposed vector has size 1x3, which definitely cannot be the result of that multiplication.

Sign in to comment.

Answers (1)

Paul
Paul on 17 Jul 2025
Edited: Paul on 17 Jul 2025
Hi Antonio,
Multiplying a matrix by [1;0;0] on the right should return the first column of the matrix, which is exactly the result.
Why would the expected result be [0;1;0]?
  1 Comment
Steven Lord
Steven Lord on 17 Jul 2025
That's not what the poster said the expected result should be. Now if the poster performed vector-matrix multiplication:
R = [0 1 0; 0 0 1; 1 0 0] % Not exactly R, but close enough
R = 3×3
0 1 0 0 0 1 1 0 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
vectorMatrix = [1 0 0]*R
vectorMatrix = 1×3
0 1 0
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
But the correct result of the matrix-vector multiplication is what MATLAB returns.
matrixVector = R*[1; 0; 0]
matrixVector = 3×1
0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Not all of the elements of the R in the original post are exactly zero or one, unlike my "close enough" approximation. So the results of the multiplications I performed are not exactly what the original poster showed, but they're close.

Sign in to comment.

Categories

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

Tags

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!