Matlab Code for Matrix Multiplication

In summary, Kerry explains that you can solve linear equations using the matrix multiplication form A=B*x if you have the matrix B and the vector x.
  • #1
cks
165
0
Hihi,
Let's say I have the linear equations ,A

2x +3y +4z
5x+6y-7z
5x-4y+3z

then I can always write as a matrix multiplication A= B*x
matrix B =
2 3 4
5 6 -7
5 -4 3

times
matrix x
x
y
z


Then., how can I tell MATLAB to express in the matrix multiplication form so that I can only have B.
 
Physics news on Phys.org
  • #2
Search the help for "left divide." Basically, you'll rearrange your equation to x = B \ A, which is MATLAB notation for A = Bx, solve for x. MATLAB doesn't handle unknowns the same way other packages do (you need the symbolic math toolbox for that), but it does make it easy to solve this kind of problem. It's more similar to a programming language than a math package (always put your unknown on the left side of the equation...).

-Kerry
 
  • #3
Ok. Thanks. Seem like if I want to perform this operation, I need symbolic math toolbox.
 
  • #4
No no, you should be able to do this without any additional packages. Did you define your A and B matricies? Actually, looking at this again, it seems you have an error. Typically, the equation would be written A * x = b, but you have B * x = A (that's fine, it's just different from what I'm used to seeing).

So continuing with your notation, how did you come up with your A matrix? B should be a matrix, and A and x are both vectors, right? The problem is that your system of equations are not equations (what are they equal to?).

For the sake of an example, we'll say that your equations are:
2x +3y +4z = -2
5x+6y-7z = 5
5x-4y+3z = 1

So you can define B:
B = [2 3 4;5 6 -7;5 -4 3];

and a:
a = [-2;5;1];

Then solve for your x vector:
x = B \ a

Hope this helps,

Kerry
 
Last edited:
  • #5
Hihi,

Kerry,

You're right, you can write
B = [2 3 4;5 6 -7;5 -4 3];
BY HAND and then put into MATLAB so that with
a = [-2;5;1];
you can solve by using the notation \
x = B \ a
easily.

But , MY problem is I want to the MATLAB to recognize A, I mean I want Matlab to get the matrix A from the equations

2x +3y +4z = -2
5x+6y-7z = 5
5x-4y+3z = 1

How can I do that?
 
  • #6
I have a lot of these types of symbolic equations where I need to extract the coefficients in front of them in order to get a Matrix.

2x +3y +4z = -2
5x+6y-7z = 5
5x-4y+3z = 1
 
  • #7
Ahh, I see. That's a different problem. Using the symbolic toolbox would be one way, but if you declare your equations as strings (like this):

eqn1='2x +3y +4z = -2';
eqn2='5x +6y -7z = 5';
eqn3='5x -4y +3z = 1';

then you can try to use sscanf(). Here is an example that builds your A matrix, b vector, and solves for x. If you have it in a different format, you can probably figure out how to modify this to suit.

Row1=sscanf(eqn1, '%fx %fy %fz = %f')';
Row2=sscanf(eqn2, '%fx %fy %fz = %f')';
Row3=sscanf(eqn3, '%fx %fy %fz = %f')';

A=[Row1(1, 1:3); Row2(1, 1:3); Row3(1, 1:3)];
b=[Row1(1, 4); Row2(1, 4); Row3(1, 4)];

x = A\b

Hope this helps,

Kerry
 
  • #8
Thanks, that's a great help.

But, I'd like to ask one more question .
I mean for putting the equation as string
eqn1='2x +3y +4z = -2';
,
let's say i have all those equations in matrix
A=[2*x+3*y+4*z;5*x+6*y-7*z;5*x-4*y+3*z]

I cannot write
eqn1 = 'A(1,1)'
because Matlab can't recognize it as
eqn1 = 2*x+3*y+4*z
So, how can I write (acutally, I tried to figure this quite some time ago?
 
  • #9
Do you already have the equations in a matrix like that? It seems to me that you would need the symbolic math toolbox just to create your A matrix...

If it's already a string, then you can do eqn1 = A(1,1).

If it's not already a string, then I'd have to think about it some more. The first thing that comes to mind is to replace x, y and z with 1, so you're left with only your coefficients or maybe some other trickery like that.

-Kerry
 
  • #10
Yes, I already have the equations in matrix form. The matrix is not a string, unfortunately.

Thanks again.
 
  • #11
So then you have the symbolic math toolbox?
 
  • #12
I don't have symbolic math toolbox. My MATLAB is just a student version.
 
  • #13
Can you post code for how you created your equation matrix? I don't see how that is possible without having the symbolic math toolbox...

-Kerry
 
  • #14
Yes, my friend has math symbolic toolbox. Do you know what is the MATLAB command code for solving this?

I have been working for my programme for quite some time, and I can't make it. I'm until this step. I hope I can get it done soon.
 

1. What is Matlab Code for Matrix Multiplication?

Matlab code for matrix multiplication is a set of instructions written in the Matlab programming language that allows users to multiply two or more matrices together. This operation is commonly used in linear algebra and is an essential tool for data analysis and scientific computing.

2. How do I write a basic matrix multiplication code in Matlab?

To write a basic matrix multiplication code in Matlab, you can use the built-in function "mtimes" or the operator "*". For example, to multiply matrices A and B, you can write "C = A*B" or "C = mtimes(A,B)". It is important to ensure that the dimensions of the matrices are compatible for multiplication.

3. Can I multiply matrices of different sizes in Matlab?

Yes, Matlab allows you to multiply matrices of different sizes as long as the dimensions are compatible. For example, a 3x4 matrix can be multiplied with a 4x2 matrix, resulting in a 3x2 matrix. However, if the dimensions are not compatible, Matlab will return an error.

4. How can I optimize my Matlab code for matrix multiplication?

To optimize your Matlab code for matrix multiplication, you can use the ".*" operator instead of the "*" operator. This will perform element-wise multiplication, which is faster than matrix multiplication. Additionally, you can pre-allocate memory for your matrices and avoid unnecessary loops to improve performance.

5. Are there any other functions I can use for matrix multiplication in Matlab?

Yes, Matlab offers a variety of functions for matrix multiplication, such as "kron" for Kronecker product, "dot" for dot product, and "cross" for cross product. These functions are useful for specific applications and can be used to perform more complex operations on matrices.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
32
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
116
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
851
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
901
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
Back
Top