Hi, I have a dataset e.g like this 44 44 0; 3 3 0; 132 133 0; 45 46 42; and want to replace the zeros with the average of each row (each row represents one variable). I know how to do it for each row individually, was wondering if there is a faster way though for when dealing with a large matrix?? Thanks !

 채택된 답변

Guillaume
Guillaume 2017년 7월 17일
편집: Guillaume 2017년 7월 17일

0 개 추천

If the 0s are supposed to indicate missing values, then you should be using NaN instead. Assuming that the mean is supposed to be taken without the 0s:
m = [44 44 0; 3 3 0; 132 133 0; 45 46 42;]
m(m == 0) = NaN; %replace 0 with NaN
mu = repmat(mean(m, 2, 'omitnan'), 1, size(m, 2)); %calculate mean and replicate to same size as M
m(isnan(m)) = mu(isnan(m)) %replace NaNs by mean

추가 답변 (1개)

Hoang Nguyen
Hoang Nguyen 2017년 7월 17일
편집: Hoang Nguyen 2017년 7월 17일

0 개 추천

Hi,
It seems you are trying to replace all the 0's in a matrix with the mean of the corresponding row. For example:
44 44 0
3 3 0
132 133 0
45 46 42
becomes
44 44 29.33
3 3 2
132 133 88.33
45 46 42
This can be done with simple matrix operations:
function[result] = replace0sWithMean(M)
% Finds the mean column vector by averaging each row
mu = mean(M, 2);
% Adds the mean column vector to the original matrix
added = M + mu;
% added(:,:) ~= mu(:) returns a boolean matrix where the 1's correspond to non-zero's in the original matrix
% Piece-wise multiply this result by the mean vector
% Subtract this quantity from the sum computed earlier in order to restore each original non-zero value
result = added - (added(:,:) ~= mu(:)) .* mu
end
As Guillaume wonderfully pointed out the case in which we don't want the 0's to be involved in the calculation of the means, use the following code snippet instead:
function[result] = replace0sWithMean(M)
temp = M;
temp(temp==0) = NaN;
mu = mean(temp, 2, 'omitnan');
added = M + mu;
result = added - (added(:,:) ~= mu(:)) .* mu;
end

댓글 수: 1

Guillaume
Guillaume 2017년 7월 17일
I would suspect that the mean is to be taken without the 0s.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by