Matlab向量化编程的利器--bsxfun函数

本文介绍了Matlab中bsxfun函数的使用,它能用于矩阵的向量化操作,如将1*n维行向量B加到m*n维矩阵A的每一行上,等效于A+repmat(B,m,1),但更节省内存。bsxfun不仅支持@plus运算,还支持@time、@rdivide等多种二元运算。" 6250365,301378,GlusterFS分布式文件系统安装配置与性能评估,"['分布式存储', '文件系统', '服务器集群', 'Linux', '性能优化']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Matlab向量化编程的利器

bsxfun函数的用法

假设A是m*n维的矩阵,B是1*n维的行向量,现在希望A的每一行都加上B,则可用下面语句

bsxfun(@plus,A,B)

实际上该语句等效为

A+repmat(B,m,1)

即把B向量扩展为与A兼容的m*n维矩阵,然后再与A相加。但在bsxfun中,这个扩展的操作是内部虚拟进行的,并不实际占用内存,因此更快。

除了@plus,还有@time,@rdivide,等二元运算,具体用法参见文献2


C = bsxfun(fun,A,B) appliesthe element-by-element binary operation specified by the functionhandlefun to arrays A and B,with singleton expansion enabled.fun can be oneof the following built-in functions:

@plus

Plus

@minus

Minus

@times

Array multiply

@rdivide

Right array divide

@ldivide

Left array divide

@power

Array power

@max

Binary maximum

@min

Binary minimum

@rem

Remainder after division

@mod

Modulus after division

@atan2

Four quadrant inverse tangent

@hypot

Square root of sum of squares

@eq

Equal

@ne

Not equal

@lt

Less than

@le

Less than or equal to

@gt

Greater than

@ge

Greater than or equal to

@and

Element-wise logical AND

@or

Element-wise logical OR

@xor

Logical exclusive OR



参考文献:

1. http://blog.sciencenet.cn/blog-242887-496374.html

2. http://blog.sina.com.cn/s/blog_9e67285801010ttn.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值