Summing matrices in cell arrays within a certain range

3 views (last 30 days)
Hi, I would like to compute the sum from different cells in an array, plus within the certain range:
d1=FFT.mBSL{1, i, i0}(min(Freq.delta):max(Freq.delta));
d2=FFT.mBSL{2, i, i0}(min(Freq.delta):max(Freq.delta));
d3=FFT.mBSL{3, i, i0}(min(Freq.delta):max(Freq.delta));
md = mean((d1+d2+d3)/3,1);
But when I try to achieve the same in a more elegant way, using cellfun, I have these errors:
s = cellfun(@sum , FFT.mBSL(:, i, i0)(min(Freq.delta):max(Freq.delta)));
Error: ()-indexing must appear last in an index expression.
or
s = cellfun(@sum , FFT.mBSL{:, i, i0}(min(Freq.delta):max(Freq.delta)))
Bad cell reference operation.
My question is, how using @sum I can choose the range of frequencies. Thanks in advance for help.

Accepted Answer

Guillaume
Guillaume on 20 Sep 2017
cellfun apply the same operation on each element of the cell array and returns an output for each of these. Therefore, it cannot be used to sum together these elements. What you can use cellfun for is filtering the matrices in each cells. The sum would have to be done afterwards:
filteredarrays = cellfun(@(arr) arr(min(Freq.delta):max(Freq.delta)), FFT.mBSL(:, i, i0), 'UniformOutput', false);
higherdim = ndims(filteredarrays{1}) + 1; %higher dimension for concatenation / sum / mean
filteredarrays = cat(higherdim, filteredarrays{:}); %concatenate all arrays in higher dimension so that they can be averaged
md = mean(mean(filteredarrays, higherdim), 1);

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!