How to sum different cells of cell array
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Hi all,
I have the following problem.
I have a cell array (2x100) where each entry is a 11x74 table. What I would llike to do is to make the mean over those 100 tables of each value.
So if for instance I have a cell array (2x2) of say 2x3 matrices:
[1,2,3;4,5,6] [3,24,35;46,58,69]
[10,2,3;24,85,6] [11,22,33;54,15,16]
I would like to end up with the mean by row of each matrix so:
[(1+3)/2, (2+24)/2,(35+3)/2;(46+4)/2,58+5)/2,(69+6)/2]
[(10+11)/2,(2+22)/2,(3+33)/2;(24+54)/2,(85+15)/2,(6+16)/2]
Is there a way to do that?
Thank you
0 Commenti
Risposte (2)
Rik
il 2 Mar 2021
Something like this should work:
data={[1,2,3;4,5,6],[3,24,35;46,58,69];
[10,2,3;24,85,6],[11,22,33;54,15,16]};
data=permute(data,[3 4 1 2]);
data=cell2mat(data);
data=mean(data,4)
You can reshape this output as needed.
2 Commenti
Rik
il 2 Mar 2021
You could fill the empty arrays with NaN and use the 'omitnan' flag. 2x100 is not big. It is too cumbersome to display in text easily, but Matlab has a max array size far exceeding 2*100*11*74.
If the order of your cells doesn't matter, you could also use cat(3,data{:}) (assuming you have already removed all empty cells and the remainder has the same size.
Star Strider
il 2 Mar 2021
I would do something like this:
C = {[1,2,3;4,5,6] [3,24,35;46,58,69]
[10,2,3;24,85,6] [11,22,33;54,15,16]};
cat3 = zeros(size(C{1},1),size(C{1},2),size(C,2));
for k1 = 1:size(C,1)
for k2 = 1:size(C,2)
cat3(:,:,k2) = C{k1,k2};
end
cat3_mean{k1,:} = mean(cat3,3)
end
cat3_mean{:} % Display Results
With the number of your matrices, two nested loops is likely the only possible solution. The cat function can do this, however it requires that the matrices to be concatenated be listed separately, and that does not appear to be possible here.
0 Commenti
Vedere anche
Categorie
Scopri di più su Matrices and Arrays in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!