matlab bsxfun memory,[转]matlab函数 bsxfun&arrayfun

bsxfun:

C = bsxfun(fun,A,B)

bsxfun可以对矩阵A和矩阵B进行对应元素的fun函数操作。其中,fun是任何标量输入输出的二元操作的函数,例如基本的加减乘除,三角函数,大小比较,以及其他任何符合条件的自定义函数。

C =

bsxfun(fun,A,B) applies the element-by-element

binary operation specified by the function

handle funto

arrays A and B,

with singleton expansion

enabled. fun can be one of 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; result

in radians

@atan2d

Four-quadrant inverse tangent; result

in degrees

@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

注意,fun不能是符号,例如+,*之类,这些符号都有对应的函数名。例如+ 对应

plus, >= 对应 ge,等等。

一般来说,如果两个矩阵一样大,我们可以直接通过 A+B

这样的方式一样实现,但是bsxfun有一个优点,就是当A,B中任何一维长度为1的时候,函数会自动将该维度和另一个矩阵对应维度的每一行(列)进行运算。如果我们自己进行这样的操作,我们或者要使用循环,或者要使用repmat来扩展矩阵,这都比bsxfun在底层内部实现慢很多,或者要消耗更多内存。

注意:A,B的维数必须要么相等,要么为1.

当其中一个元素的维数=1时,系统自动将其扩展为另一元素的维数

The corresponding dimensions

of A and B must

be equal to each other or equal to one. Whenever a dimension

of A or B is

singleton (equal to

one), bsxfun virtually replicates

the array along that dimension to match the other array. In the

case where a dimension

of A or B is

singleton, and the corresponding dimension in the other array is

zero, bsxfun virtually diminishes

the singleton dimension to zero.

The size of the output

array C is equal to:

max(size(A),size(B)).*(size(A)>0 &

size(B)>0).

For example:如何将一个矩阵的每行或每列元素分别扩大不同的倍数?如[1

2 3;4 5 6 ;7 8 9],第一列元素乘以1,第二列元素以2,第三列元素乘以4。

利用bsxfun函数,可以给出下列代码:

a = [1,2,3;

4,5,6;

7,8,9];

acol = bsxfun(@times,a,[1 2 4])

此处bsxfun函数将[1 2 4]扩展为3*3矩阵[1

2 4;1

2 4;1

2 4],每个元素与a中对应元素相乘。得到

[1  4

12

4  10

24

7  16

36]

[转]Matlab:用内建函数代替for循环_cairo_新浪博客

http://blog.sina.com.cn/s/blog_83b901bd0101ea8l.html

[转载]matlab学习笔记  函数bsxfun

repmat_yoki_新浪博客

http://blog.sina.com.cn/s/blog_780976a001012m0a.html

********************************************************************************************

arrayfun:

Apply function to each element of array

[B1,...,Bm] =

arrayfun(func,A1,...,An)

[B1,...,Bm] =

arrayfun(func,A1,...,An,Name,Value)

利用arrayfun函数可以避免无谓的循环,从而大大提高代码的简介性。

1、A=ARRAYFUN(FUN, B)

FUN是函数句柄,对B中的每一个元素调用FUN函数(计算顺序随意),结果存放于A中,size(A)==size(B)

FUN函数可接受numeric, logical, char, struct,

cell的自变量类型。

2、[A, B, ...] = ARRAYFUN(FUN, C, ...)

FUN函数的返回值是一个向量,对B中的每一个元素调用FUN函数,计算结果放在A,B…中。

3、 A = ARRAYFUN(FUN, B, C,...)

FUN函数接受参数不唯一,分别调用B, C,...中的元素,A(i,j...)=Fun(B(i,j...),C(i,j,...)...),

B,C...大小必须相等。

例1、Fun调用B中的一个数值

>> s=[1 2;3 4];

>> f=@(x) x^2;

>> arrayfun(f,s)

ans =

14

916

例2、Fun的接受参数是一个向量

>> ss=num2cell(s,2);

>> f=@(x) sum(x{:}.^2);

>> arrayfun(f,ss)

ans =

5

25

例3、Fun函数返回值是向量

>> F=@(x) x{:}.^2;

>>

cell2mat(arrayfun(F,ss,'UniformOutput',false))

ans =

14

916

例4、Fun函数参数不唯一

>> f=@(x,y) x^2+y^2;

>> [X,Y]=meshgrid(-2:2,-1:5);

>> Z=arrayfun(f,X,Y);

>> mesh(X,Y,Z)

例5、Fun函数的参数不唯一,而且都是向量

>>

F=@(f,h)sum(diff(f{:}).^2)+sum(diff(h{:}).^2);

>> f=[1,2,3;3,4,5;1,2,5];

>> h=[2,4;4,5;];

>> ff=num2cell(f,2);

>> hh=num2cell(h,2);

>> fff=repmat(ff',length(hh),1);

>> hhh=repmat(hh,1,length(ff));

>> arrayfun(F,fff,hhh)

ans =

6614

3311

C2=num2cell(A)是把A中的每一个元素作为cell的元素,这样每个元素是一个数;C2=num2cell(A,1)是把矩阵A的每一列作为cell的元素,这样cell的每个元素是一个列向量。同样的还有C2=num2cell(A,2)表示把A中的每一行作为一个cell元素

转自:

arrayfun用法_2096428102_新浪博客

http://blog.sina.com.cn/s/blog_7cf4f4460101bnhh.html

********************************************************************************************

cellfun:

1

2

[A1,...,Am] = cellfun(func,C1,...,Cn)

[A1,...,Am] = cellfun(func,C1,...,Cn,Name,Value)

和arrayfun的用法类似,不过是对cell的对应元素进行操作。

structfun:

1

2

[A1,...,An] =

structfun(func,S)

[A1,...,An] =

structfun(func,S,Name,Value)

类似的用法,对结构体S的所有域进行func操作。

spfun:

1

f = spfun(fun,S)

这个函数可以对一个稀疏矩阵S的每个有值的元素进行fun操作。

这个函数的用途不仅仅是可以提升速度,更重要的是能够保持返回的f中,没有数据的地方依然为0.

例如:

1

2

S = spdiags([1:4]',0,4,4)

f = spfun(@exp,S)

S =

(1,1) 1

(2,2) 2

(3,3) 3

(4,4) 4

f =

(1,1) 2.7183

(2,2) 7.3891

(3,3) 20.0855

(4,4) 54.5982

而直接运行

1

exp(S)

的话,没有数据的地方都变成1了。

1

full(exp(S))

ans =

2.7183 1.0000 1.0000 1.0000

1.0000 7.3891 1.0000 1.0000

1.0000 1.0000 20.0855 1.0000

1.0000 1.0000 1.0000 54.5982

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值