# I need average of all elements with same position of cell arrays.

9 views (last 30 days)
zhoug zho on 19 May 2021
Commented: Rik on 19 May 2021
I have M = 349*1 cell array where each cell has three columns and different rows as shows below (first three cells).
The highlighted elements in bold letters are first element of each cell in column 2.
I need to take the average of all these first elements of all cells that are present in column 2.
Similarly next I need to take the average of second element of each cell that are present in column 2 (highlighted in underline), but as you can see that cell 1 don’t has second element (so I need to consider the value of value 2nd element here equal to zero), but 2nd and 3rd cells have second elements.
Similarly for 3rd , 4th , 5thelements of column 2 of all cells, I need average of all elements with same position.
Cell 1:
1 7203.40000000000 -115
Cell 2:
2 7203.38000000000 -117.980000000000
2 7203.45000000000 -118.580000000000
2 7203.47000000000 -122.730000000000
2 7203.63000000000 -127.960000000000
2 7203.65000000000 -132.570000000000
Cell 3:
3 7203.39000000000 -118.710000000000
3 7203.48000000000 -118.360000000000
3 7203.63000000000 -129.740000000000
3 7203.66000000000 -138.930000000000
3 7203.81000000000 -128.020000000000
3 7204.47000000000 -130.520000000000
3 7204.75000000000 -129.160000000000
3 7204.77000000000 -136.890000000000
Can anyone help. Thanks.

Rik on 19 May 2021
You could reshape your data to a 3D array, which would allow you simply use mean.
FillValue=0;
M=cell(3,1);
M{1}=[1 6000 -115];
M{2}=[2 6200 -116;2 6200 -117];
M{3}=[3 6400 -118;3 6400 -119;3 6300 -120];
n_rows=cellfun('size',M,1);
max_n_rows=max(n_rows);
for c=find(n_rows<max_n_rows).'
%extend all cells to the same size
M{c}((end+1):max_n_rows,2)=FillValue;
end
M_3D=cat(3,M{:});
format compact;M_3D
M_3D =
M_3D(:,:,1) = 1 6000 -115 0 0 0 0 0 0 M_3D(:,:,2) = 2 6200 -116 2 6200 -117 0 0 0 M_3D(:,:,3) = 3 6400 -118 3 6400 -119 3 6300 -120
mean(M_3D(:,2,:),3)
ans = 3×1
6200 4200 2100
zhoug zho on 19 May 2021
ohk my badluck. that was browser issue, thats why i could not get it.
now it is ok. thank you so much
Rik on 19 May 2021
You're welcome

KSSV on 19 May 2021
You run a loop for each celll....extract the column you want and find the average using mean.
Let A be your cell array of size 349*1.
n = length(A) ;
M = zeros(n,1) ;
for i= 1:n
T = A{i} ; % extract cell array
M(i) = mean(T(:,2)) ; % mean of second column of each cell
end
Like shown above, you can follow.
zhoug zho on 19 May 2021
Edited: zhoug zho on 19 May 2021
thanks for the consideration.
i dont need the mean of all elemnts of second coloumn of a cell,
as i said earlier , i only need element wise mean of all cells, e.g first, ineed mean of all first elemnts of each cell from coloumn 2, then i need mean of second elemnts of all cells from colomn 2 and so on. so i need these means in separate for each position. (description is in question).
KSSV on 19 May 2021
Yes I got it....you can use indexing and extract the elements you want.