Sum selected columns of a matrix
Show older comments
Hi all,
I have a matrix that looks as follow:
0.0486947932834768 0.590504451038576 0 0.0579865082127525 0.0490083905415712 0.672222222222222 0 0.0948620001376558
0.636363636363636 0 -0.0250828729281768 0.00478066895757889 0 -0.0254237288135594 -0.0299024510443391 0.662162162162162
-0.0480580939947781 0.0438218390804598 0.549848942598187 0 -0.00331564986737398 -0.00459016393442624 0.596590909090909 0
0 -0.0554096692111959 -0.0255076123713734 0.632241813602015 0.608591885441527 0 0.00929780525880808 0.0107530809183949
0 0 0 0 0 0 0 0
(see the picture for a better view of the matrix:
)
)What I would like to do is to average the columns where a 0 appears, a part from the last row. So for instance in the above case, since in the first column a zero, a part from the last row, appears in position 4, I would like to average this column with column number 6 (where another 0 appears in row 4). The second column should be averaged with the fifth as in both columns a 0 appears in second row and so on.
The expected result should look as follows:
1 0.1076.
2 0.1233
3 0.1463
4 0.1279
where 1,2,3,4 represent the row position of the 0 in the columns averaged.
How can I achieve this result?
Thank you
Accepted Answer
More Answers (1)
Another approach
A = [0.0486947932834768 0.590504451038576 0 0.0579865082127525 0.0490083905415712 0.672222222222222 0 0.0948620001376558;
0.636363636363636 0 -0.0250828729281768 0.00478066895757889 0 -0.0254237288135594 -0.0299024510443391 0.662162162162162;
-0.0480580939947781 0.0438218390804598 0.549848942598187 0 -0.00331564986737398 -0.00459016393442624 0.596590909090909 0;
0 -0.0554096692111959 -0.0255076123713734 0.632241813602015 0.608591885441527 0 0.00929780525880808 0.0107530809183949;
0 0 0 0 0 0 0 0];
[row,col] = find(A(1:end-1,:)==0);
R = zeros(size(A));
R(:,col) = repmat(row',size(A,1),1);
meancol = accumarray(R(:),A(:),[],@mean)
Categories
Find more on Loops and Conditional Statements 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!