Taking averages of only a few rows out of several?
3 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Voss
on 15 Jan 2022
A = (1:6).'.*(1:100)
mean(A(1:3,:))
mean(A(1:3,:),'all')
mean(A(end-2:end,:))
mean(A(end-2:end,:),'all')
0 Comments
More Answers (1)
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)
% Get 3 means -- one for each row.
meanFirst3 = mean(A(1:3,:), 2)
meanLast3 = mean(A(end-2:end,:), 2)
% Get 1 mean covering all 3 rows
meanFirst3 = mean2(A(1:3,:))
meanLast3 = mean2(A(end-2:end,:))
If you don't have mean2 (in the Image Processing Toolbox), you can use mean(A(1:3,:), 'all').
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!