Average every 3 rows of 1 column in a 12 x 8 array

5 次查看(过去 30 天)
I have a 12 x 8 array of data and want to average every 3 rows of the 4th column in the array, resulting in a 4 x 1 array. To be extremely clear, I want rows 1 to 3 averaged, then 4 to 6 averaged, then 7 to 9 averaged then 10 to 12 averaged.
I need to also apply this to other arrays that have different numbers of rows but still in multiples of 3 (e.g., could be 15 x 8 or 18 x 8 swapped for the original 12 x 8).
%Here is the example data in the 12 x 8 array
34 6 4 -6.60874766440390 -40.7725049965035 16217 0.289000000000000 1.02200000000000
35 6 5 -6.54326464638770 -40.5611336664881 16525 0.286000000000000 1.23700000000000
36 6 6 -6.54628693952691 -40.6729573224369 16386 0.202000000000000 1.13000000000000
172 6 4 -6.61176995754311 -39.6642486096006 16209 0.232000000000000 1.03300000000000
173 6 5 -6.62184426800714 -40.0039787602111 16212 0.236000000000000 1.02700000000000
174 6 6 -6.66113407881686 -39.9370137005201 16069 0.271000000000000 1.16200000000000
310 6 4 -6.64803747521362 -40.3153910672626 16143 0.239000000000000 1.13100000000000
311 6 5 -6.61479225068232 -40.5411679705423 16113 0.285000000000000 1.06400000000000
312 6 6 -6.63695573370318 -40.0871548121586 16266 0.249000000000000 1.02200000000000
448 6 4 -6.76792176973558 -40.3663142652769 16176 0.297000000000000 1.01800000000000
449 6 5 -6.68631985497693 -40.6696972594111 16075 0.277000000000000 1.19400000000000
450 6 6 -6.68631985497693 -40.8846682513060 16046 0.238000000000000 1.17900000000000
  1 个评论
VBBV
VBBV 2023-6-20
移动:VBBV 2023-6-21
Data = [
34 6 4 -6.60874766440390 -40.7725049965035 16217 0.289000000000000 1.02200000000000
35 6 5 -6.54326464638770 -40.5611336664881 16525 0.286000000000000 1.23700000000000
36 6 6 -6.54628693952691 -40.6729573224369 16386 0.202000000000000 1.13000000000000
172 6 4 -6.61176995754311 -39.6642486096006 16209 0.232000000000000 1.03300000000000
173 6 5 -6.62184426800714 -40.0039787602111 16212 0.236000000000000 1.02700000000000
174 6 6 -6.66113407881686 -39.9370137005201 16069 0.271000000000000 1.16200000000000
310 6 4 -6.64803747521362 -40.3153910672626 16143 0.239000000000000 1.13100000000000
311 6 5 -6.61479225068232 -40.5411679705423 16113 0.285000000000000 1.06400000000000
312 6 6 -6.63695573370318 -40.0871548121586 16266 0.249000000000000 1.02200000000000
448 6 4 -6.76792176973558 -40.3663142652769 16176 0.297000000000000 1.01800000000000
449 6 5 -6.68631985497693 -40.6696972594111 16075 0.277000000000000 1.19400000000000
450 6 6 -6.68631985497693 -40.8846682513060 16046 0.238000000000000 1.17900000000000];
K = 1;
for k = 1:4
Mean(k) = mean(Data(K:K+2,4));
K = K + 3;
end
Mean.'
ans = 4×1
-6.5661 -6.6316 -6.6333 -6.7135

请先登录,再进行评论。

采纳的回答

James Tursa
James Tursa 2023-6-20
编辑:James Tursa 2023-6-21
E.g.,
Data = [
34 6 4 -6.60874766440390 -40.7725049965035 16217 0.289000000000000 1.02200000000000
35 6 5 -6.54326464638770 -40.5611336664881 16525 0.286000000000000 1.23700000000000
36 6 6 -6.54628693952691 -40.6729573224369 16386 0.202000000000000 1.13000000000000
172 6 4 -6.61176995754311 -39.6642486096006 16209 0.232000000000000 1.03300000000000
173 6 5 -6.62184426800714 -40.0039787602111 16212 0.236000000000000 1.02700000000000
174 6 6 -6.66113407881686 -39.9370137005201 16069 0.271000000000000 1.16200000000000
310 6 4 -6.64803747521362 -40.3153910672626 16143 0.239000000000000 1.13100000000000
311 6 5 -6.61479225068232 -40.5411679705423 16113 0.285000000000000 1.06400000000000
312 6 6 -6.63695573370318 -40.0871548121586 16266 0.249000000000000 1.02200000000000
448 6 4 -6.76792176973558 -40.3663142652769 16176 0.297000000000000 1.01800000000000
449 6 5 -6.68631985497693 -40.6696972594111 16075 0.277000000000000 1.19400000000000
450 6 6 -6.68631985497693 -40.8846682513060 16046 0.238000000000000 1.17900000000000];
mean(reshape(Data(:,4),3,[])).'
ans = 4×1
-6.5661 -6.6316 -6.6333 -6.7135
The reshape( ) puts the data you want averaged into individual columns of three elements each, and the mean( ) automatically operates on columns giving the result you want. This naturally results in a row vector, so the final transpose reshapes it into a column vector. The method shown works for any sized matrix where the number of rows is a multiple of 3.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by