MATLAB already has functionality to do this in the bsxfun function. bsxfun will take two matrices and duplicate singleton dimensions until the matrices are the same size, then perform a binary operation on the two matrices. So, for your example, you would simply do the following:

C = bsxfun(@times,mat,vec);
Answer from MrAzzaman on Stack Overflow
🌐
MathWorks
mathworks.com › symbolic math toolbox › symbolic computations in matlab › operators and elementary operations
times - Symbolic array multiplication - MATLAB
Create a 3-by-3 symbolic Hilbert matrix and a 3-by-3 diagonal matrix. ... Multiply the matrices by using the element-wise multiplication operator .*. This operator multiplies each element of the first matrix by the corresponding element of the second matrix.
🌐
MathWorks
mathworks.com › matlab › mathematics › elementary math › arithmetic operations
Array vs. Matrix Operations - MATLAB & Simulink
MATLAB® has two different types of arithmetic operations: array operations and matrix operations. You can use these arithmetic operations to perform numeric computations, for example, adding two numbers, raising the elements of an array to a given power, or multiplying two matrices. Matrix operations follow the rules of linear algebra. By contrast, array operations execute element by element operations and support multidimensional arrays.
Top answer
1 of 2
4

MATLAB already has functionality to do this in the bsxfun function. bsxfun will take two matrices and duplicate singleton dimensions until the matrices are the same size, then perform a binary operation on the two matrices. So, for your example, you would simply do the following:

C = bsxfun(@times,mat,vec);
2 of 2
2

Referencing MrAzzaman, bsxfun is the way to go with this. However, judging from your function name, this looks like it's homework, and so let's stick with what you have originally. As such, you need to only write two for loops. You would use the second for loop to index into both the vector and the columns of the matrix at the same time. The outer most for loop would access the rows of the matrix. In addition, you are referencing A and B, which are variables that don't exist in your code. You are also initializing the output matrix C to be 2 x 3 always. You want this to be the same size as mat. I also removed your checking of the length of the vector because you weren't doing anything with the result.

As such:

function C = lab11(mat, vec)
[a, b] = size(mat);
C = zeros(a,b);
for i = 1:a
      for k = 1:b
          C(i,k) = mat(i,k) * vec(k);
      end
  end
end

Take special note at what I did. The outer-most for loop accesses the rows of mat, while the inner-most loop accesses the columns of mat as well as the elements of vec. Bear in mind that the number of columns of mat need to be the same as the number of elements in vec. You should probably check for this in your code.


If you don't like using the bsxfun approach, one alternative is to take the vector vec and make a matrix out of this that is the same size as mat by stacking the vector vec on top of itself for as many times as we have rows in mat. After this, you can do element-by-element multiplication. You can do this stacking by using repmat which repeats a vector or matrices a given number of times in any dimension(s) you want. As such, your function would be simplified to:

function C = lab11(mat, vec)
rows = size(mat, 1);
vec_mat = repmat(vec, rows, 1);
C = mat .* vec_mat;
end

However, I would personally go with the bsxfun route. bsxfun basically does what the repmat paradigm does under the hood. Internally, it ensures that both of your inputs have the same size. If it doesn't, it replicates the smaller array / matrix until it is the same size as the larger array / matrix, then applies an element-by-element operation to the corresponding elements in both variables. bsxfun stands for Binary Singleton EXpansion FUNction, which is a fancy way of saying exactly what I just talked about.

Therefore, your function is further simplified to:

function C = lab11(mat, vec)
C = bsxfun(@times, mat, vec);
end

Good luck!

🌐
MathWorks
mathworks.com › matlab › mathematics › elementary math › arithmetic operations
times - Multiplication - MATLAB
This MATLAB function multiplies arrays A and B by multiplying corresponding elements.
🌐
TutorialsPoint
tutorialspoint.com › matlab › matlab_matrix_multiplication.htm
MATLAB - Matrix Multiplication
Each element in the (i, j)th position, ... jth column of the second matrix. Matrix multiplication in MATLAB is performed by using the * operator....
🌐
MathWorks
mathworks.com › matlabcentral › answers › 344326-i-want-to-perform-element-wise-matrix-multiplication
I Want to perform element wise matrix multiplication - MATLAB Answers - MATLAB Central
June 12, 2017 - You see you can multiply only same classes. Class of a is uint8, so you need to convert b into the same class. ... https://www.mathworks.com/matlabcentral/answers/344326-i-want-to-perform-element-wise-matrix-multiplication#comment_460933
🌐
MATLABSolutions
matlabsolutions.com › home › notes › element by element operations in matlab programming
Element-by-Element Operations in MATLAB Programming | MATLAB Dot Operators Explained - Notes
Without the dot (.), MATLAB performs matrix operations, which follow rules of linear algebra instead of element-level math. ... Here, each element of A is multiplied by the corresponding element of B.
🌐
MathWorks
mathworks.com › matlab › mathematics › linear algebra
mtimes - Matrix multiplication - MATLAB
For more information, see Run MATLAB Functions with Distributed Arrays (Parallel Computing Toolbox). ... The mtimes operator supports operations directly on tables and timetables without indexing to access their variables when one of the operands is a scalar. All elements in the table or timetable input must have data types that support matrix multiplication...
Find elsewhere
🌐
Dummies
dummies.com › article › technology › programming-web-design › matlab › how-to-employ-matrix-multiplication-in-matlab-146892
How to Employ Matrix Multiplication in MATLAB | dummies
March 26, 2016 - You supply a function nam to perform an element-by-element math operation on two objects. You can use the @times function name, which performs multiplication. The two inputs are a row vector and a column vector.
Author:
🌐
MathWorks
mathworks.com › matlabcentral › answers › 409930-multidimensional-matrix-multiplication-elementwise
Multidimensional matrix multiplication elementwise - MATLAB Answers - MATLAB Central
July 11, 2018 - How can you have an element-by-element product between the square and rectangular matrix? Can you give a specific example, say with a 2x3x3 matrix and a 5x3 matrix, showing the full inputs and desired output? Side Note: In general, it is best to have your 2D pages as the first two dimensions, not the last two. That way the 2D pages are contiguous in memory. ... https://www.mathworks.com/matlabcentral/answers/409930-multidimensional-matrix-multiplication-elementwise#comment_588343
🌐
YouTube
youtube.com › watch
Matrix Multiplication and Element Wise Multiplication in MATLAB (4K UHD) - YouTube
In this video, we will see how to perform a matrix multiplication both in MATLAB and by analytical methods. We will then define what is an element wise opera...
Published: March 16, 2021
🌐
MathWorks
mathworks.com › matlabcentral › answers › 112673-element-wise-multiplication-of-vectors-within-matrices-when-multiplying-matrices
Element wise multiplication of vectors within matrices when multiplying matrices. - MATLAB Answers - MATLAB Central
January 16, 2014 - https://www.mathworks.com/matl... the desired operation is to use a for loop to iterate through the third dimension of M1 and M2 and perform 2-D matrix multiplication at each level....
🌐
MathWorks
mathworks.com › matlabcentral › answers › 2173842-elementwise-multiplication-of-two-3d-matrices
Elementwise multiplication of two 3D matrices - MATLAB Answers - MATLAB Central
February 10, 2025 - To multiply two 3D matrices element-wise in MATLAB, the appropriate operator is the dot multiplication operator `.*`, not `pagemtimes`. The `pagemtimes` function is designed for matrix multiplication, which requires that the inner dimensions ...
🌐
YouTube
youtube.com › watch
Matlab Tutorial - 39 - Multiplying and Dividing Matrices Element-by-Element - YouTube
Get more lessons like this at http://www.MathTutorDVD.comLearn how to multiply and divide matrices element by element. This operation is different from the ...
Published: March 23, 2018
🌐
YouTube
youtube.com › watch
Matrix multiplication in matlab | Elementwise matrix multiplication in matlab | MATLAB TUTORIAL - YouTube
Matrix multiplication standard and elementwise, division, addition and subtraction in matlab is covered in this video. Basically this video will give you ide...
Published: October 3, 2022
🌐
MathWorks
mathworks.com › matlabcentral › answers › 450725-matrix-multiplication-element-wise
Matrix multiplication element wise - MATLAB Answers - MATLAB Central
March 18, 2019 - I have two matrices A and B of the order 49*49 each. I want to generate third matrix C whose elements are product of individual elements of A and B (not matrix multiplication) Like C(i,j) = A(i,j)*B(j,i). What could be the matlab command for this.