Any way to do this using simple matrix math?

  • Thread starter khkwang
  • Start date
  • Tags
    Matrix
In summary, the conversation discusses a problem with multiplying a vector and a matrix element-wise in MATLAB. The solution proposed is to use the element-wise multiplication operator, represented by a dot, instead of a for loop. This will result in a more efficient code without compromising the desired output.
  • #1
khkwang
60
0

Homework Statement


The following notation is as follows: a comma ',' represents a column and a semi-colon ';' represents a new row.

I have a vector:
A = [a1, a2, a3, a4, a5]

and a matrix

B = [b1, b2, b3, b4, b5;
b6, b7, b8, b9, b10;
b11, b12, b13, b14, b15]

What I want is:

C = [a1*b1, a2*b2, a3*b3, a4*b4, a5*b5;
a1*b6, a2*b7, a3*b8,a4*b9, a5*b10;
a1*b11, a2*b12, a3*b13, a4*b14, a5*b15]

Basically, each row of B needs to be multiplied with A element-wise.

Homework Equations


Just some basic matrix math.


The Attempt at a Solution


I'm doing this in MATLAB (which does matrix math much faster than loops). I can only think to do a for loop for the number of rows in B, and doing an element-wise multiplication of A and the current row of B. But The number of rows in B is actually quite large and would cause the program to run much slower than I'd like.
 
Physics news on Phys.org
  • #2
Is there a more efficient way to do this?



Thank you for your post. I understand your concern about the efficiency of your code. In this case, I would recommend using the element-wise multiplication operator in MATLAB, which is represented by the dot '.'. This allows you to perform element-wise multiplication without using a for loop. The code would look something like this:

C = A .* B;

This will multiply each element in A with the corresponding element in B, resulting in a matrix with the same dimensions as B. I hope this helps. Let me know if you have any further questions.
 

Related to Any way to do this using simple matrix math?

1. Can I use matrix multiplication to solve any type of mathematical problem?

No, matrix multiplication is limited to specific types of problems, such as linear equations and transformations.

2. What are the benefits of using matrix math?

Matrix math allows for efficient and organized calculations, especially for systems of equations or transformations.

3. Is there a limit to the size of matrices that can be used in calculations?

Yes, the size of matrices that can be used depends on the computing power and memory capacity of the computer or device.

4. Are there any shortcuts or tricks for solving matrix equations?

Yes, there are various techniques such as Gaussian elimination and LU decomposition that can be used to simplify and speed up matrix calculations.

5. Can matrix math be applied to real-world problems?

Yes, matrix math is commonly used in fields such as engineering, physics, and economics to model and solve real-world problems.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
920
  • Calculus and Beyond Homework Help
Replies
8
Views
1K
  • Linear and Abstract Algebra
Replies
6
Views
4K
  • Programming and Computer Science
7
Replies
235
Views
10K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Quantum Interpretations and Foundations
2
Replies
38
Views
4K
  • Calculus and Beyond Homework Help
Replies
5
Views
2K
  • Set Theory, Logic, Probability, Statistics
Replies
17
Views
2K
Replies
8
Views
3K
  • Calculus and Beyond Homework Help
Replies
1
Views
2K
Back
Top