question about vector multiplication

The Matlab vector v1 has a dimension of n-by-1 and the vector v2 has a dimension of 1-by-n, which of the following is true?
A) The+operation v1*v2 does+not+return+an+error
B) The+operation v2*v1 does+not+return+an+error
C) The+operation v1.*v2 does+not+return+an+error
D) None+of+the+above

6 comentarios

Steven Lord
Steven Lord el 7 de Dic. de 2019
Since this sounds like a homework (or maybe an exam) question, what do you think the answer is and why do you think that's the answer?
Judy Zhuo
Judy Zhuo el 7 de Dic. de 2019
the answer is A but idk why I tried with matlab and all three work
David Goodmanson
David Goodmanson el 7 de Dic. de 2019
Editada: David Goodmanson el 7 de Dic. de 2019
Hello JZ,
If anyone is saying that A is correct but B is not, they are mistaken. Both A and B are true, with A returning an NxN matrix and B returning a scalar. That's standard matrix algebra.
I am a bit surprised that C, element-by-element multiplication, returns an answer. I thought that it would error out. For the product of an NxM matrix and an MxN matrix it does error out for N~=M, N >1, M >1, i.e. v1 and v2 are matrices rather than simple vectors.
Judy Zhuo
Judy Zhuo el 7 de Dic. de 2019
The question said A and B are vectors
v1.*v2 is an example of implicit expansion as of R2016b.
The question said A and B are vectors
Yes, but the question talks about returning an error, not about whether the result is an array, a vector, or a scalar.
>> v1 = rand(5,1); v2 = rand(1,5);
>> v1*v2
ans =
0.772181449374705 0.136904501442802 0.406952626197035 0.883582709529619 0.764391769792849
0.126134670907917 0.0223631430789796 0.0664750954870026 0.144331898126657 0.124862238539422
0.77674644648747 0.137713856102018 0.409358456543161 0.888806290750275 0.768910715728357
0.766002014144726 0.135808913741177 0.403695959778422 0.87651177804287 0.758274672010212
0.388436651747121 0.0688681736560934 0.204712656160906 0.444475724597195 0.384518146507958
>> v2*v1
ans =
2.46493297354767
>> v1.*v2
ans =
0.772181449374705 0.136904501442802 0.406952626197035 0.883582709529619 0.764391769792849
0.126134670907917 0.0223631430789796 0.0664750954870026 0.144331898126657 0.124862238539422
0.77674644648747 0.137713856102018 0.409358456543161 0.888806290750275 0.768910715728357
0.766002014144726 0.135808913741177 0.403695959778422 0.87651177804287 0.758274672010212
0.388436651747121 0.0688681736560934 0.204712656160906 0.444475724597195 0.384518146507958
No errors.
Judy Zhuo
Judy Zhuo el 7 de Dic. de 2019
Thanks

Iniciar sesión para comentar.

Respuestas (1)

Sourav Bairagya
Sourav Bairagya el 10 de Dic. de 2019

0 votos

The first two options of your question are performing matrix multiplication of the vectors A and B by considering them nX1 and 1Xn matrices respectively. In first case it will produce a nXn matrix and in 2nd case it will give 1X1 matrix i.e. a scalar.
In 3rd option, it is the case of element-wise multiplication.
C = A.*B performs the element-wise multiplication of vectors A and B, where either A and B are of same size or having compatible sizes. If A and B have compatible sizes, then the two vector implicitly expand to match each other.
Like, if one of them is a scalar, then the scalar is combined with each element of the other vector. Also, vectors with different orientations (one row vector and one column vector) implicitly expand to form a matrix. Hence, in these cases, this elememt-wise multiplications will sucessfully performed without throwing any error.
To know more about the compatible sizes of two vectors, you can leverage the following link:

Categorías

Etiquetas

Preguntada:

el 7 de Dic. de 2019

Respondida:

el 10 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by