# explanation for syntax of arrayfun

25 Ansichten (letzte 30 Tage)
Abirami am 24 Aug. 2014
Kommentiert: Abirami am 24 Aug. 2014
Hello
can anyone explain the syntax of the following code pls...
B= arrayfun(@(x) x, A, 'uni', 0);
##### 0 Kommentare-2 ältere Kommentare anzeigen-2 ältere Kommentare ausblenden

Melden Sie sich an, um zu kommentieren.

### Akzeptierte Antwort

Geoff Hayes am 24 Aug. 2014
Abirami - see the documentation at arrayfun for details on the usage of this function. From your example,
B = arrayfun(@(x) x, A, 'uni', 0);
arrayfun will apply the anonymous function @(x) x on each element of A. The uni string must be equivalent to UniformOutput being set to 0 (or false) which from the documentation states Requests that the arrayfun function combine the outputs into cell arrays B1,...,Bm. The outputs of function func can be of any size or type. So here, func is the anonymous function.
Suppose
A = [1:5]'
A =
1
2
3
4
5
then
B = arrayfun(@(x) x, A, 'uni', 0)
B =
[1]
[2]
[3]
[4]
[5]
The output B is a 5x1 cell array. B is identical to A since our anonymous function would take in an element (x) and then just return it. If we had
B = arrayfun(@(x) 2*x, A, 'uni', 0)
B =
[ 2]
[ 4]
[ 6]
[ 8]
[10]
So each element in B is double that of A. Note that if we removed the uni option (and its value)
B = arrayfun(@(x) 2*x, A)
B =
2
4
6
8
10
Then B is no longer a cell array.
##### 1 Kommentar-1 ältere Kommentare anzeigen-1 ältere Kommentare ausblenden
Abirami am 24 Aug. 2014
thank u so much....understood it perfectly..

Melden Sie sich an, um zu kommentieren.

### Kategorien

Mehr zu Structures finden Sie in Help Center und File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by