Function which returns the outer product of two vectors
64 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dave Smith
le 15 Mar 2013
Commenté : George Zipfel
le 11 Jan 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
Réponse acceptée
James Tursa
le 15 Mar 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 commentaires
Katie
le 21 Mar 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
le 22 Mar 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
Plus de réponses (1)
Jason McNeill
le 20 Fév 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 commentaire
Torsten
le 20 Fév 2023
It's the usual matrix multiplication:
a=[8 21 1 9];
b=[11 13 2 7 5];
b.'*a
Voir également
Catégories
En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!