can anyone explain the syntax of the following code pls...
B= arrayfun(@(x) x, A, 'uni', 0);
thanks in advance
Best Answer
Abirami - see the documentation atarrayfun 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 ofA. Theuni string must be equivalent toUniformOutput being set to 0 (or false) which from the documentation statesRequests 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 outputB is a 5x1 cell array. B is identical toA 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 inB is double that ofA. Note that if we removed theuni option (and its value)
The anonymous function used in cellfun is applied to each element of A, so the input it expects in your case would be a 1 x 2 cell of strings, not the parent cell array of cell arrays:
Best Answer