user3071257
user3071257

Reputation: 37

Can someone please give me an example of arrayfun in matlab

I am confused by the parameters and when trying to use it I keep getting told to use a function handle, (I do not even know what that is)?!

labelsAsChars = arrayfun(char,labels);

I want to convert an array of Ints into an array of Chars. labelsAsChars is what I want to store it in char is the function that turns an Int into a Char i.e. char(97) returns 'a'. labels is the 20 by 20 matrix of Ints.

Upvotes: 1

Views: 9872

Answers (1)

octopushugs
octopushugs

Reputation: 157

arrayfun is for using a function for each element of an array. Basically if you have:

A = arrayfun(fun, S)

A is an array that you're assigning the value output, fun is the function that you are applying, and S is the array of things that you are applying the function to. When it talks about the function handle it's referring to "fun" in the example, which needs to be a valid function. In psuedocode that means something like this:

s = [1, 2, 3]
def add1(x)
return x+1;
end
a = arrayfun(add1, s);
puts a

the output would look like [2, 3, 4].

There's some additional reading about it here that I found helpful: http://function.name/in/Matlab/arrayfun

Upvotes: 2

Related Questions