Function which returns the outer product of two vectors
64 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Dave Smith
el 15 de Mzo. de 2013
Comentada: George Zipfel
el 11 de En. de 2024
Write a function which returns the outer product between two vectors. The function should accept two input arguments: the vectors to be multiplied. The function will return the matrix containing the outer product of the two vectors.
Requirements: (a) Your function should perform all operations on individual array elements (i.e. do not employ Matlab commands which operate on entire matrices/rows/columns).
(b) Your function should determine all necessary vector and matrix sizes.
(c) The size of the output matrix will be determined by the order of the input arguments, regardless of whether the input arguments are row or column vectors. The length of the first input vector determines the number of rows in the output vector and the length of the second input vector determines the number of columns in the output matrix.
Here is the code that I attempted. Let me know what I should do to fulfill all of the requirements.
a=[8 21 1 9];
b=[11 13 2 7 5];
b=b';
A=b*a
Respuesta aceptada
James Tursa
el 15 de Mzo. de 2013
You can't use the last two lines since they employ commands that operate on entire vectors. I would suggest you write out the result of your last line in symbols, or just look at the first Google link for "outer product":
Then write two for loops, nested, to obtain each element according to the pattern you see in the link.
4 comentarios
Katie
el 21 de Mzo. de 2013
Do you have any guidance for where I should go next in my code? I went to the wiki link above and got even more confused.
James Tursa
el 22 de Mzo. de 2013
Look, here is the very first equation in that link:
Outer Product of [u1 u2 u3 u4] and [v1 v2 v3] =
[ u1v1 u1v2 u1v3 ]
[ u2v1 u2v2 u2v3 ]
[ u3v1 u3v2 u3v3 ]
[ u4v1 u4v2 u4v3 ]
Are you really telling me you can't write two nested for loops to calculate that result? And then expand that to any size of u and v? Here, I will even get you started
for x=1:4
for y=1:3
outerproduct(x,y) = _____?
end
end
Más respuestas (1)
Jason McNeill
el 20 de Feb. de 2023
You can use meshgrid and elementwise multiplication
a=[8 21 1 9];
b=[11 13 2 7 5];
[aa,bb]=meshgrid(a,b);
A=bb.*aa
1 comentario
Torsten
el 20 de Feb. de 2023
It's the usual matrix multiplication:
a=[8 21 1 9];
b=[11 13 2 7 5];
b.'*a
Ver también
Categorías
Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!