相关疑难解决方法(0)

matlab/octave - 广义矩阵乘法

我想做一个函数来推广矩阵乘法.基本上,它应该能够进行标准矩阵乘法,但它应该允许通过任何其他函数更改两个二元运算符product/sum.

目标是在CPU和内存方面尽可能高效.当然,它总是比A*B效率低,但操作员的灵活性才是最重要的.

以下是我在阅读各种 有趣 线程后可以提出的一些命令:

A = randi(10, 2, 3);
B = randi(10, 3, 4);

% 1st method
C = sum(bsxfun(@mtimes, permute(A,[1 3 2]),permute(B,[3 2 1])), 3)
% Alternative: C = bsxfun(@(a,b) mtimes(a',b), A', permute(B, [1 3 2]))

% 2nd method
C = sum(bsxfun(@(a,b) a*b, permute(A,[1 3 2]),permute(B,[3 2 1])), 3)

% 3rd method (Octave-only)
C = sum(permute(A, [1 3 2]) .* permute(B, [3 2 1]), 3)

% 4th method (Octave-only): multiply nxm A with nx1xd B to …
Run Code Online (Sandbox Code Playgroud)

matlab matrix octave matrix-multiplication

10
推荐指数
1
解决办法
1785
查看次数

bsxfun在矩阵乘法中的实现

一如既往地想从你身上学到更多东西,我希望能通过以下代码获得一些帮助.

我需要完成以下任务:

1)我有一个向量:

x = [1 2 3 4 5 6 7 8 9 10 11 12]
Run Code Online (Sandbox Code Playgroud)

2)和矩阵:

A =[11    14    1
    5     8    18
    10    8    19
    13    20   16]
Run Code Online (Sandbox Code Playgroud)

我需要能够将eachxevery值相乘A,这意味着:

new_matrix = [1* A
              2* A
              3* A
               ...
              12* A]
Run Code Online (Sandbox Code Playgroud)

这将给我这个new_matrix大小(12*m x n)假设A (mxn).在这种情况下(12*4x3)

我怎么能用bsxfunmatlab 做这个呢?并且,这种方法会比一个快for-loop吗?

关于我for-loop,我在这里也需要一些帮助...我无法存储每个"new_matrix"循环运行:(

for i=x
new_matrix = A.*x(i)
end
Run Code Online (Sandbox Code Playgroud)

提前致谢!! …

performance matlab matrix matrix-multiplication bsxfun

7
推荐指数
2
解决办法
1621
查看次数