Matlab中行和列的矩阵乘法而没有for循环 [英] Matrix multiplication of row and column without for loop in matlab

查看:256
本文介绍了Matlab中行和列的矩阵乘法而没有for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下问题,没有for循环,我想不出办法.

I have the following problem and can not think of a way around without for loops.

考虑两个矩阵:

a=rand(N,3), b=rand(3,N)

我想要得到的是:b(:, i)*a(i, :)(3 * 3矩阵)在所有所有i的三维空间中串联.

What I want to get is: b(:, i)*a(i, :) (a 3*3 matrix) concatenated in the third dimension for all i.

因此,在上面的示例中,结果应为(3 * 3 * N)矩阵.

Thus for the above example the result should be a (3*3*N) matrix.

推荐答案

Matlab R2016b版本:

Matlab R2016b version:

c = reshape(a.',[1,3,N]) .* reshape(b,[3,1,N]);

早期的Matlab版本:

Earlier Matlab versions:

c = repmat(reshape(a.',[1,3,N]),[3,1,1]) .* repmat(reshape(b,[3,1,N]),[1,3,1]);

编辑:这是Matlab R2016b(Win7x64)的快速基准测试.向量化的速度提高了约50倍.

edit: Here is a quick benchmark on Matlab R2016b (Win7x64). Speedup of vectorization is around a factor of 50.

这篇关于Matlab中行和列的矩阵乘法而没有for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆