[转]matlab函数 bsxfun&arrayfun

分类: Matlab |
bsxfun:
|
C = bsxfun(fun,A,B)
|
bsxfun可以对矩阵A和矩阵B进行对应元素的fun函数操作。其中,fun是任何标量输入输出的二元操作的函数,例如基本的加减乘除,三角函数,大小比较,以及其他任何符合条件的自定义函数。
C =
bsxfun(fun,A,B)
@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
The size of the output
array
max(size(A),size(B)).*(size(A)>0 &
size(B)>0).
For example:
利用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
4
7
[转]Matlab:用内建函数代替for循环_cairo_新浪博客
http://blog.sina.com.cn/s/blog_83b901bd0101ea8l.html
[转载]matlab学习笔记
http://blog.sina.com.cn/s/blog_780976a001012m0a.html
********************************************************************************************
arrayfun:
Apply function to each element of array
|
[B1,...,Bm]
[B1,...,Bm] |
利用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,
B,C...大小必须相等。
例1、Fun调用B中的一个数值
>> s=[1 2;3 4];
>> f=@(x) x^2;
>> arrayfun(f,s)
ans =
例2、Fun的接受参数是一个向量
>> ss=num2cell(s,2);
>> f=@(x) sum(x{:}.^2);
>> arrayfun(f,ss)
ans =
例3、Fun函数返回值是向量
>> F=@(x) x{:}.^2;
>>
cell2mat(arrayfun(F,ss,'UniformOutput',false))
ans =
例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 =
C2=num2cell(A)是把A中的每一个元素作为cell的元素,这样每个元素是一个数;C2=num2cell(A,1)是把矩阵A的每一列作为cell的元素,这样cell的每个元素是一个列向量。同样的还有C2=num2cell(A,2)表示把A中的每一行作为一个cell元素
********************************************************************************************
cellfun:
1 2 |
[A1,...,Am]
[A1,...,Am] |
和arrayfun的用法类似,不过是对cell的对应元素进行操作。
structfun:
1 2 |
[A1,...,An]
[A1,...,An] |
类似的用法,对结构体S的所有域进行func操作。
spfun:
1 |
f =
|
这个函数可以对一个稀疏矩阵S的每个有值的元素进行fun操作。
这个函数的用途不仅仅是可以提升速度,更重要的是能够保持返回的f中,没有数据的地方依然为0. 例如:
1 2 |
S =
f = |
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
今天激素分泌不正常,听歌: