How to take mean of the first numbers of column in matrix and add to each number in same column?
3 views (last 30 days)
Show older comments
Hi guys I have a matrix:
A = [...
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1]
Now I would like to take the mean of the first 4 numbers in column 1 and add to each number in column 1. In the second column I want to take the mean of the first 4 numbers and add to each number in column 2. And so on. Can this be done in a easy manner or do I have to import the data and calculate the mean myself and then add to the columns in the matrix? Help is greatly appreciated. Thx in advance.
Kind regards
A MatLab Novice
Accepted Answer
Image Analyst
on 5 Dec 2014
This is how I do it (hope it's not your homework I just did):
A = [...
64 2 3 61 60 6 7 57
9 55 54 12 13 51 50 16
17 47 46 20 21 43 42 24
40 26 27 37 36 30 31 33
32 34 35 29 28 38 39 25
41 23 22 44 45 19 18 48
49 15 14 52 53 11 10 56
8 58 59 5 4 62 63 1]
meanOfRows1to4 = mean(A(1:4,:), 1)
rows = size(A, 1);
output = A + repmat(meanOfRows1to4, [rows, 1])
output =
96.5 34.5 35.5 93.5 92.5 38.5 39.5 89.5
41.5 87.5 86.5 44.5 45.5 83.5 82.5 48.5
49.5 79.5 78.5 52.5 53.5 75.5 74.5 56.5
72.5 58.5 59.5 69.5 68.5 62.5 63.5 65.5
64.5 66.5 67.5 61.5 60.5 70.5 71.5 57.5
73.5 55.5 54.5 76.5 77.5 51.5 50.5 80.5
81.5 47.5 46.5 84.5 85.5 43.5 42.5 88.5
40.5 90.5 91.5 37.5 36.5 94.5 95.5 33.5
More Answers (1)
Azzi Abdelmalek
on 5 Dec 2014
Edited: Azzi Abdelmalek
on 5 Dec 2014
Edit
b=bsxfun(@plus,A,mean(A(1:4,:))')
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!