Octave:两个向量的笛卡尔积的向量化实现

Octave : Vectorized implementation of the cartesian product of two vectors

是否可以在不使用 for 循环的情况下实现下面代码的功能?

基本上我们从矩阵 a 和 b 中取出每对行向量,将它们广播到矩阵中,进行分量乘法并将结果设置为结果向量。

谢谢

m = 4;
n1 = 3;
n2 = 2;

% result is a 3d array of dimensions m * n1 * n2
result = reshape(1:24, m, n1, n2);

a = reshape(1:12, m, n1)
b = reshape(1:8, m, n2)

for i = 1:m
 mat = a(i, :)' .* b(i, :);

 result(i, :, :) = mat;
endfor

广播也可以应用于乘法多维数组:

 result  = a .* reshape (b, m, 1, n2);