Taking averages of only a few rows out of several?

1 view (last 30 days)
I have a data set that is 6X100. I need to take the average of the first three rows for all 100 and then of the last three rows for all 100 columns. How do I take averages of only a few rows?

Accepted Answer

Voss
Voss on 15 Jan 2022
A = (1:6).'.*(1:100)
A = 6×100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 156 162 168 174 180
mean(A(1:3,:))
ans = 1×100
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60
mean(A(1:3,:),'all')
ans = 101
mean(A(end-2:end,:))
ans = 1×100
5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150
mean(A(end-2:end,:),'all')
ans = 252.5000

More Answers (1)

Image Analyst
Image Analyst on 16 Jan 2022
That's kind of ambiguous. Do you want a mean from each row, so you'll get 3 values? Or do you want all 300 values to be averaged into a single value.
A = (1:6).'.*(1:100)
A = 6×100
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45 48 51 54 57 60 63 66 69 72 75 78 81 84 87 90 4 8 12 16 20 24 28 32 36 40 44 48 52 56 60 64 68 72 76 80 84 88 92 96 100 104 108 112 116 120 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95 100 105 110 115 120 125 130 135 140 145 150 6 12 18 24 30 36 42 48 54 60 66 72 78 84 90 96 102 108 114 120 126 132 138 144 150 156 162 168 174 180
% Get 3 means -- one for each row.
meanFirst3 = mean(A(1:3,:), 2)
meanFirst3 = 3×1
50.5000 101.0000 151.5000
meanLast3 = mean(A(end-2:end,:), 2)
meanLast3 = 3×1
202.0000 252.5000 303.0000
% Get 1 mean covering all 3 rows
meanFirst3 = mean2(A(1:3,:))
meanFirst3 = 101
meanLast3 = mean2(A(end-2:end,:))
meanLast3 = 252.5000
If you don't have mean2 (in the Image Processing Toolbox), you can use mean(A(1:3,:), 'all').

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!