将矩阵乘以向量 [英] Multiply matrix to vector

查看:23
本文介绍了将矩阵乘以向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有矩阵 A(n,m),我想将它乘以向量 b(n),结果 B[n,m]=A[n,m]*b[n].可以通过创建一个新矩阵 C=b*ones(1,m) 然后使用点乘法来实现:B=A.*C,但它浪费内存(A的大小是5000*1000).可以使用循环.也许有更优雅的方式来做到这一点?

I've got matrix A(n,m) and I want multiply it to vector b(n), so that the result B[n,m]=A[n,m]*b[n]. It is possible to do it by creating a new matrix C=b*ones(1,m) and then use dot multiplication: B=A.*C, but it is waste of memory (size of A is 5000*1000). It is possible to use loops. Maybe there are more elegant way to do it?

推荐答案

使用 bsxfun,仅此而已:

Use bsxfun, which is just for that:

B = bsxfun(@times, A, b(:));

这篇关于将矩阵乘以向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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