Matrix does not want to multiply and gives an error when I multiply a 3x3 matrix with a 3x1 matrix

5 views (last 30 days)
Gihahn on 8 May 2024
Answered: Stephen23 on 16 May 2024
A=[9,29.3, 140.99; 29.3, 140.99,827.693; 140.99, 827.693, 5400.863]
A = 3x3
1.0e+03 * 0.0090 0.0293 0.1410 0.0293 0.1410 0.8277 0.1410 0.8277 5.4009
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ainv=inv(A); % inverse matrix
B=[4.6;-16; -267.29] % B matrix used in A^-1*B
B = 3x1
4.6000 -16.0000 -267.2900
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
values = B * Ainv %multiply the matrix to get a and b
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each element of the matrix individually, use TIMES (.*) for elementwise multiplication.
the multiplication does not want to multiply
Steven Lord on 16 May 2024
There's no need to call inv here and multiply. Yes, I know that's probably what you were taught in school, but to use A to solve a system of equations A*x = b compute x = A\b instead.
format longg
A=[9,29.3, 140.99; 29.3, 140.99,827.693; 140.99, 827.693, 5400.863]
A = 3x3
1.0e+00 * 9 29.3 140.99 29.3 140.99 827.693 140.99 827.693 5400.863
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B=[4.6;-16; -267.29]
B = 3x1
4.6 -16 -267.29
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
values = A\B
values = 3x1
-1.76108777348174 2.72268694776457 -0.420774080489157
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
checkThatAxEqualsB = A*values-B % Should contain values very close to 0
checkThatAxEqualsB = 3x1
1.0e+00 * 8.88178419700125e-15 5.6843418860808e-14 5.6843418860808e-14
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Voss on 8 May 2024
Do Ainv*B not B*Ainv
A=[9,29.3, 140.99; 29.3, 140.99,827.693; 140.99, 827.693, 5400.863]
A = 3x3
1.0e+03 * 0.0090 0.0293 0.1410 0.0293 0.1410 0.8277 0.1410 0.8277 5.4009
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ainv=inv(A) % inverse matrix
Ainv = 3x3
0.8808 -0.4791 0.0504 -0.4791 0.3313 -0.0383 0.0504 -0.0383 0.0047
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
B=[4.6;-16; -267.29] % B matrix used in A^-1*B
B = 3x1
4.6000 -16.0000 -267.2900
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
values = Ainv*B %multiply the matrix to get a and b
values = 3x1
-1.7611 2.7227 -0.4208
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

sai charan sampara on 8 May 2024
Edited: sai charan sampara on 8 May 2024
Hello Gihahn,
In line 2 "mat1" is not defined. If you want to have the inverse of "A" replace it with A. Also the size of "B" is 3x1 and "A" is 3x3 matrix. So trying to multiply "B" with "A" will give an error. You can take the transpose of "B" and then multiply with "A" or change the order of multiplication for the dimensions to agree for multiplication.
A=[9,29.3, 140.99; 29.3, 140.99,827.693; 140.99, 827.693, 5400.863]
A = 3x3
1.0e+03 * 0.0090 0.0293 0.1410 0.0293 0.1410 0.8277 0.1410 0.8277 5.4009
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Ainv=inv(A); % inverse matrix
B=[4.6;-16; -267.29] % B matrix used in A^-1*B
B = 3x1
4.6000 -16.0000 -267.2900
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
values = B' * Ainv
values = 1x3
-1.7611 2.7227 -0.4208
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
values = Ainv*B
values = 3x1
-1.7611 2.7227 -0.4208
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Ahmed Abed on 16 May 2024
You are multiplying [3x1] matrix by [3x3] one, which is mathmatically incorrect.
If you try values = Ainv*B then you will get an answer (not sure if it is what you wanting).

Stephen23 on 16 May 2024
Read the INV documentation! And then use the recommended function MLDIVIDE:
A = [9, 29.3, 140.99; 29.3, 140.99,827.693; 140.99, 827.693, 5400.863];
B = [4.6; -16; -267.29]
B = 3x1
4.6000 -16.0000 -267.2900
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
C = A\B
C = 3x1
-1.7611 2.7227 -0.4208
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Categories

Find more on Programming in Help Center and File Exchange

R2022b

Community Treasure Hunt

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

Start Hunting!