I need average of all elements with same position of cell arrays.
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
zhoug zho
el 19 de Mayo de 2021
Comentada: Rik
el 19 de Mayo de 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.
0 comentarios
Respuesta aceptada
Rik
el 19 de Mayo de 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
mean(M_3D(:,2,:),3)
6 comentarios
Más respuestas (1)
KSSV
el 19 de Mayo de 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.
2 comentarios
Ver también
Categorías
Más información sobre Matrices and Arrays en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!