如何从两个数组中所有元素的乘积创建矩阵?

How to create a matrix from product of all elements in two arrays?

我正在阅读 documentation for matlab on element-wise multiplication,我遇到了这个例子:

Create a row vector a and a column vector b, then multiply them. The 1-by-3 row vector and 6-by-1 column vector combine to produce a 6-by-3 matrix with all combinations of elements multiplied.

文档确实显示了输出,但他们是如何得到大小为 6,3 的输出矩阵的?这是使用文档中解释的方法将大小为 6,1 的列向量 b 与大小为 1,3 的行向量 a 相乘得到的。

这叫做广播。当一个维度为1而另一个维度较大时,单位维度被扩展,如同repmat:

 6 1 : column
 1 3 : row
 ------
 6 3 : result

给出

a = 1:3
b = [1:6]'
a .* b

大致相当于

a2 = repmat(a, 6, 1)
b2 = repmat(b, 1, 3)
a2 .* b2

当然广播的内存效率更高。