cellfun is just like looping through the cell matrix and
executing specified function separately on each cell.
It's usually faster than doing the same thing explicitly
in loop, but the basic difference is that its easy to
write and read - its immediately clear what the call is
doing. But you could just as well write the loop yourself.
In your specific case you could use cellfun this way:
mean_a = mean(cellfun(@(x) mean(x(:)), a));
If you have thousands of cells and you want to do something
to each of them you either use a loop or a cellfun
BTW: @(x) means that you want the content of each cell
to be understood as x so that mean(x(:)) gives you what
you want - mean of the whole matrix content of the cell.
cellfun is just like looping through the cell matrix and
executing specified function separately on each cell.
It's usually faster than doing the same thing explicitly
in loop, but the basic difference is that its easy to
write and read - its immediately clear what the call is
doing. But you could just as well write the loop yourself.
In your specific case you could use cellfun this way:
mean_a = mean(cellfun(@(x) mean(x(:)), a));
If you have thousands of cells and you want to do something
to each of them you either use a loop or a cellfun
BTW: @(x) means that you want the content of each cell
to be understood as x so that mean(x(:)) gives you what
you want - mean of the whole matrix content of the cell.
It of course depends in what you want to do. cellfun is intended to act separately on each cell in your cell array. If you want to take a global mean of your cell array values, and insist on using cellfun then this should work:
mean_a = mean(cell2mat(cellfun(@mean, a,'UniformOutput',0)))
Explanation of cellfun()
MATLAB: Using get in cellfun for a cell array of objects - Stack Overflow
What is this Matlab function? cellfun(@(x)str2num(x),
You'll want to check out the documentation for cellfun and the section about anonymous functions.
More on reddit.combsxfun - matlab: cellfun logic - Stack Overflow
I'm reading someone else's script that clearly wasn't described, and with little experience in Matlab. From what I understand, it means apply a function in each cell? What are the @ symbols, and what is the purpose of the two (x) in between str2num?
Thanks!
You'll want to check out the documentation for cellfun and the section about anonymous functions.
func_name = @(x) do_stuff(x)
is an anonymous function. Just a one-line function with less computational overhead than a function in a separate file. num2str does just that: takes a num (numeric type) and turns it into a str (string). cellfun applies the same function (in this case the anonymous function) to each cell in a cell array. So something like this {10, 15, 20} will become {'10', '15', '20'}.