An assignment of vectorization

Say you have two column vectors vv and ww, each with 7 elements (i.e., they have dimensions 7x1). Consider the following code:
z = 0;
for i = 1:7
z = z + v(i) * w(i)
end
A) z = sum (v .* w);
B) z = w' * v;
C) z = v * w;
D) z = w * v;
According to the solutions, answers (A) AND (B) are the right answers, can someone please help me understand why?

6 commentaires

madhan ravi
madhan ravi le 2 Fév 2019
Multiplication operator with dot represents element wise operation and the one without is matrix multiplication.https://www.mathworks.com/help/matlab/matlab_prog/array-vs-matrix-operations.html
John D'Errico
John D'Errico le 2 Fév 2019
Surely this question was given to you as a homework assignment, AFTER you have been taught about matrix multiplication. The point is, you have been given the clues in your school work. And Madhan gave you the perfect clue here. Remember that a vector IS a matrix. Just one with one row or column.
Luis URBINA
Luis URBINA le 1 Août 2019
I was struggling with this question, also, but finally I got the point.
The given code calculates z as the accumulated sum of the multiplied v(i) by w(i). So, z is accumulating scalar numbers in each (i) interaction in the for loop. At the end of the loop, the value of z=21.
The right answers are A and B. Thank you both.
John D'Errico
John D'Errico le 1 Août 2019
All of them hinge on what a dot product means, and how that solves your problem.
Sinehan S
Sinehan S le 29 Nov 2020
Modifié(e) : Sinehan S le 29 Nov 2020
In question they said that each column vector v and w as 7x1 dimension.
They gave v(i)*w(i). So, we cannot multiply 7*1 dimension and 7*1 dimension.
So, we should take transpose for anyone v(i) or w(i).
Then, only we get (1*7)(7*1) and (7*1)(1*7)dimension.
Finally, we get solution by multiply them.
Amr Soror
Amr Soror le 14 Oct 2021
for A) x .* y
Element-by-element multiplication. If both operands are matrices, the number of rows and columns must both agree, or they must be broadcastable to the same shape.

Réponses (0)

Cette question est clôturée.

Produits

Version

R2018b

Clôturé :

le 14 Oct 2021

Community Treasure Hunt

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

Start Hunting!

Translated by