Averaging elements for a cell within a cell array

7 ビュー (過去 30 日間)
L'O.G.
L'O.G. 2022 年 7 月 11 日
回答済み: Hrusheekesh 2022 年 7 月 11 日
I have a 200×1 cell array where each element is {130×1 double}. How do I take the mean of every element in each cell-within-the-cell? The first element, second element, and so on? So that I have a vector that is 130 x 1 of all the averages?

採用された回答

Stephen23
Stephen23 2022 年 7 月 11 日
編集済み: Stephen23 2022 年 7 月 11 日

その他の回答 (2 件)

Aiswarya
Aiswarya 2022 年 7 月 11 日
In order to apply a function on each cell of a cell array, you can use the function cellfun(). Refer to the below documentation on how to use it:
For your question of taking mean of each element in the cell array you can use the code snippet below:
A = cellfun(@mean,C) % C is your cell array and A is the resultant array
  1 件のコメント
L'O.G.
L'O.G. 2022 年 7 月 11 日
That takes the mean for all 200 cells. I want the mean for all the first elements in the 200 cells, all the second elements, all the third elements, and so on. So in the end the vector should be 130 x 1.

サインインしてコメントする。


Hrusheekesh
Hrusheekesh 2022 年 7 月 11 日
where c is your cell.
clc
c={[1,2,3];[2,3,40];[34,21,35];[2,7,5];[33 67 89]};
p=[];
n=size(c{1});
m=size(c);
for i=1:n(2)
sum=0;
for j=1:m(1)
arr=c{j};
sum=sum+arr(i);
end
p=[p sum/m(1)];
end
p

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by