MATLAB Answers

Mean Value of Cell array having uneven vector

25 views (last 30 days)
Tukaram Zore
Tukaram Zore on 30 Sep 2020 at 11:20
Commented: Tukaram Zore on 5 Oct 2020 at 6:40
Hi, I have a cell array of dimension mxn, each element of the cell is a vector of 1xp but p is not of same size. I want to create an array of mxp containing the mean value of each element (with index p) across all the cell element (index n). Can someone help me in this regard.
Thank You


Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 30 Sep 2020 at 12:08
An alternative approach
C = {1, [1 2 3]; [2 3], [3 5]};
mp = max(cellfun(@numel, C(:)));
C = cellfun(@(x) {[x nan(1, mp-numel(x))]}, C);
M = cell2mat(arrayfun(@(i) {nanmean(cellfun(@(x) x(i), C), 2)}, 1:mp));
>> M
M =
1.0000 2.0000 3.0000
2.5000 4.0000 NaN

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by