加载中…
个人资料
李想想一想
李想想一想
  • 博客等级:
  • 博客积分:0
  • 博客访问:2,577
  • 关注人气:5
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

[转]matlab函数 bsxfun&arrayfun

(2015-12-14 21:03:15)
分类: Matlab
在使用matlab进行矩阵计算的时候,经常会遇到要使用for循环的情况。但其实很多操作可以用内部的一些函数代替。 bsxfun, arrayfun, cellfun, spfun, structfun

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 fun to arrays 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 and must be equal to each other or equal to one. Whenever a dimension of or 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 or 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 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 =
         4
        16

例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 =
         4
        16

例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 =
            14
            11
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

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有