Function which returns the outer product of two vectors

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

 採用された回答

James Tursa
James Tursa 2013 年 3 月 15 日

0 投票

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 件のコメント

Katie
Katie 2013 年 3 月 20 日
編集済み: Katie 2013 年 3 月 20 日
I have the same problem and I got x=a'*b. Without any for loops. Is it that simple or am I missing something? My code asks for inputs of what a and b are.
Matt Kindig
Matt Kindig 2013 年 3 月 20 日
Your solution also uses vector/matrix operations (specifically transpose and matrix multiplication). So this would also be unacceptable for your assignment.
Katie
Katie 2013 年 3 月 21 日
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
James Tursa 2013 年 3 月 22 日
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

サインインしてコメントする。

その他の回答 (1 件)

Jason McNeill
Jason McNeill 2023 年 2 月 20 日

1 投票

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
A = 5×4
88 231 11 99 104 273 13 117 16 42 2 18 56 147 7 63 40 105 5 45

1 件のコメント

Torsten
Torsten 2023 年 2 月 20 日
It's the usual matrix multiplication:
a=[8 21 1 9];
b=[11 13 2 7 5];
b.'*a
ans = 5×4
88 231 11 99 104 273 13 117 16 42 2 18 56 147 7 63 40 105 5 45

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeCreating and Concatenating Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by