I have a cell array (2 x 6) called "output", each cell in row #1 {1 -> 6, 2} contains a 1024 x 1024 x 100 matrix. I want to apply movmedian to each cell in row #1. I would like to apply this function in dimension = 3 with window size = 5.
output = cellfun(@movmedian(5,3), output,'uniform', 0);
This is the code that I have come up with so far, however, it produces an "unbalenced or unexpected parenthesis or bracket" error. I am unsure what is causing this error. I am also somewhat unsure how to instruct matlab to perform this operation only on row 1 of the cell array, please help!
Thank you for your time!!
The function handle passed as the first argument to cellfun
will be sequentially passed the contents of each cell (i.e. each 3-D matrix). Since you need to also pass the additional parameters needed by movmedian
, you should create an anonymous function like so:
@(m) movmedian(m, 5, 3)
Where the input argument m
is the 3-D matrix. If you want to apply this to the first row of output
, you just have to index the cell array like so:
output(1, :)
This will return a cell array containing the first row of output
, with :
indicating "all columns". You can use the same index in the assignment if you'd like to store the modified matrices back in the same cells of output
.
Putting it all together, here's the solution:
output(1, :) = cellfun(@(m) movmedian(m, 5, 3), output(1, :),...
'UniformOutput', false);
...and a little trick to avoid having to specify 'UniformOutput', false
is to encapsulate the results of the anonymous function in a cell array:
output(1, :) = cellfun(@(m) {movmedian(m, 5, 3)}, output(1, :));