Sum selected columns of a matrix

6 Ansichten (letzte 30 Tage)
federico nutarelli
federico nutarelli am 30 Aug. 2022
Beantwortet: Bruno Luong am 30 Aug. 2022
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

Akzeptierte Antwort

Karim
Karim am 30 Aug. 2022
One approach is shown below, there are some comments to explain the steps.
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];
% first find row (R) and column (C) indexes for the zeros, omit the last row
[R,C] = find( A(1:end-1,:) == 0 );
% determine the number of rows (omit the last one)
numrow = size(A,1) - 1;
% initialize a vector for the results
MyMean = zeros( numrow,1);
% loop over the number of rows
for i = 1:numrow
% first find the indexes for the columns with zeros on the i-th row
idx = R == i;
% extract the data from the matrix using the indexes
currData = A(:, unique( C(idx) ) );
% take the mean of the data
MyMean(i) = mean( currData , 'all');
end
% print the result
MyMean
MyMean = 4×1
0.1075 0.1233 0.1463 0.1279
  1 Kommentar
Bruno Luong
Bruno Luong am 30 Aug. 2022
Verschoben: Bruno Luong am 30 Aug. 2022
currData = A(:, unique( C(idx) ) )
I think unique statement is not needed.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Bruno Luong
Bruno Luong am 30 Aug. 2022
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)
meancol = 4×1
0.1075 0.1233 0.1463 0.1279

Produkte


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by