0% found this document useful (0 votes)
1K views4 pages

Apply Function To Each Element of Array - MATLAB Arrayfun - MathWorks India

The document discusses the MATLAB arrayfun function which applies a function to each element of an array. It provides the syntax, describes how arrayfun works, and gives examples of using arrayfun to count elements, get array sizes, calculate means, and check for equality across structures.

Uploaded by

rahuldbajaj2011
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
1K views4 pages

Apply Function To Each Element of Array - MATLAB Arrayfun - MathWorks India

The document discusses the MATLAB arrayfun function which applies a function to each element of an array. It provides the syntax, describes how arrayfun works, and gives examples of using arrayfun to count elements, get array sizes, calculate means, and check for equality across structures.

Uploaded by

rahuldbajaj2011
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 4

4/11/13

Apply function to each element of array - MATLAB arrayfun - MathWorks India

arrayfun
Apply function to each element of array

Syntax
[ B 1 , . . . , B m ]=a r r a y f u n ( f u n c , A 1 , . . . , A n ) [ B 1 , . . . , B m ]=a r r a y f u n ( f u n c , A 1 , . . . , A n , N a m e , V a l u e )

Description
[ B 1 , . . . , B m ]=a r r a y f u n ( f u n c , A 1 , . . . , A n )calls the function specified by function handle f u n cand passes elements from arrays A 1 , . . . , A n , w here nis the number of inputs to function f u n c . Output arrays B 1 , . . . , B m , w here mis the number of outputs from function f u n c , contain the combined outputs from the function calls. The i th iteration corresponds to the syntax [ B 1 ( i ) , . . . , B m ( i ) ]=f u n c ( A 1 { i } , . . . , A n { i } ) . The a r r a y f u nfunction does not perform the calls to function f u n cin a specific order. [ B 1 , . . . , B m ]=a r r a y f u n ( f u n c , A 1 , . . . , A n , N a m e , V a l u e )calls function f u n cw ith additional options specified by one or more N a m e , V a l u epair arguments. Possible values for N a m eare ' U n i f o r m O u t p u t 'or ' E r r o r H a n d l e r ' .

Input Arguments
f u n c Handle to a function that accepts ninput arguments and returns m output arguments. If function f u n ccorresponds to more than one function file (that is, if f u n crepresents a set of overloaded functions), MATLAB determines w hich function to call based on the class of the input arguments. A 1 , . . . , A n Arrays that contain the ninputs required for function f u n c . Each array must have the same dimensions. Arrays can be numeric, character, logical, cell, structure, or user-defined object arrays. If any input Ais a user-defined object array, and you overloaded the s u b s r e for s i z emethods, a r r a y f u nrequires that: The s i z emethod returns an array of type d o u b l e . The object array supports linear indexing. The product of the sizes returned by the s i z emethod does not exceed the limit of the array, as defined by linear indexing into the array.

Name-Value Pair Arguments


Specify optional comma-separated pairs of N a m e , V a l u earguments. N a m eis the argument name and V a l u eis the corresponding value. N a m emust appear inside single quotes ( ' ' ). You can specify several name and value pair arguments in any order as N a m e 1 , V a l u e 1 , . . . , N a m e N , V a l u e N . ' U n i f o r m O u t p u t ' Logical value, as follow s:

t r u e
www.mathworks.in/help/matlab/ref/arrayfun.html

Indicates that for all inputs, each output from function


1/4

4/11/13

Apply function to each element of array - MATLAB arrayfun - MathWorks India

( 1 )

f u n cis a cell array or a scalar value that is alw ays of the same type. The a r r a y f u nfunction combines the outputs in arrays B 1 , . . . , B m , w here mis the number of function outputs. Each output array is of the same type as the individual function outputs.

f a l s e ( 0 )

Requests that the a r r a y f u nfunction combine the outputs into cell arrays B 1 , . . . , B m . The outputs of function f u n ccan be of any size or type.

Default: t r u e ' E r r o r H a n d l e r ' Handle to a function that catches any errors that occur w hen MATLAB attempts to execute function f u n c . Define this function so that it rethrow s the error or returns valid outputs for function f u n c . MATLAB calls the specified error-handling function w ith tw o input arguments: A structure w ith these fields: i d e n t i f i e r Error identifier. m e s s a g e i n d e x Error message text. Linear index corresponding to the element of the input cell array at the time of the error.

The set of input arguments to function f u n cat the time of the error.

Output Arguments
B 1 , . . . , B m Arrays that collect the moutputs from function f u n c . Each array Bis the same size as each of the inputs A 1 , . . . , A n . Function f u n ccan return output arguments of different classes. How ever, if U n i f o r m O u t p u tis t r u e(the default): The individual outputs from function f u n cmust be scalar values (numeric, logical, character, or structure) or cell arrays. The class of a particular output argument must be the same for each set of inputs. The class of the corresponding output array is the same as the class of the outputs from function f u n c .

Examples
To run the examples in this section, create a nonscalar structure array w ith arrays of different sizes in field f 1 .

www.mathworks.in/help/matlab/ref/arrayfun.html

2/4

4/11/13

Apply function to each element of array - MATLAB arrayfun - MathWorks India

s ( 1 ) . f 1=r a n d ( 3 ,6 ) ; s ( 2 ) . f 1=m a g i c ( 1 2 ) ; s ( 3 ) . f 1=o n e s ( 5 ,1 0 ) ;

Count the number of elements in each f 1field. c o u n t s=a r r a y f u n ( @ ( x )n u m e l ( x . f 1 ) ,s ) The syntax @ ( x )creates an anonymous function. This code returns c o u n t s= 1 8 1 4 4 5 0

Compute the size of each array in the f 1fields. [ n r o w s ,n c o l s ]=a r r a y f u n ( @ ( x )s i z e ( x . f 1 ) ,s ) This code returns n r o w s= 3 n c o l s= 6 1 2 1 0 1 2 5

Compute the mean of each column in the f 1fields of s . Because the output is nonscalar, set U n i f o r m O u t p u tto f a l s e . a v e r a g e s=a r r a y f u n ( @ ( x )m e a n ( x . f 1 ) ,s ,' U n i f o r m O u t p u t ' , f a l s e ) This code returns a v e r a g e s= [ 1 x 6d o u b l e ] [ 1 x 1 2d o u b l e ] [ 1 x 1 0d o u b l e ]

Create additional nonscalar structures tand u , and test for equality betw een the arrays in fields f 1across structures s ,t , and u . t=s ; t ( 1 ) . f 1 ( : ) = 0 ; u=s ; u ( 2 ) . f 1 ( : ) = 0 ; s a m e=a r r a y f u n ( @ ( x , y , z )i s e q u a l ( x . f 1 ,y . f 1 ,z . f 1 ) ,s ,t ,u ) This code returns s a m e=
www.mathworks.in/help/matlab/ref/arrayfun.html 3/4

4/11/13

Apply function to each element of array - MATLAB arrayfun - MathWorks India

See Also
c e l l 2 m a t| c e l l f u n| f u n c t i o n _ h a n d l e| s p f u n| s t r u c t f u n

Tutorials
Anonymous Functions

Was this topic helpful?

Yes

No

Try MATLAB, Simulink, and Other Products


Get trial now

www.mathworks.in/help/matlab/ref/arrayfun.html

4/4

You might also like