HOW TO TAKE AVERAGE OF ROW AND COLUMN AT SOME POINT
1 view (last 30 days)
Show older comments
abdul wahab aziz
on 24 Aug 2016
Commented: abdul wahab aziz
on 24 Aug 2016
I HAVE A MATRIX
2 5 3 500 4 5,
3 4 2 2 3 5,
4 5 5 1 2 2
code must scan first where 500 exists and take average of that row and column wher 500 exists excluding that 500,
here ans should be like avg row at first 2+5+3+4+5/5 and avg col of that point 2+1/2,
3 Comments
Accepted Answer
Star Strider
on 24 Aug 2016
This works:
M = [2 5 3 500 4 5
3 4 2 2 3 5
4 5 5 1 2 2];
[r,c] = find(M == 500);
RowAvg = mean(M(r,M(r,:)~=500));
ColAvg = mean(M(M(:,c)~=500,c));
3 Comments
Star Strider
on 24 Aug 2016
The ‘r’ and ‘c’ are the row and column indices of ‘500’ respectively.
In the event of more than one ‘500’, the easiest way to modify my code would be to add a loop:
M = [2 5 3 500 4 5
3 4 2 2 3 5
4 5 500 1 2 2];
[r,c] = find(M == 500);
for k1 = 1:size(r,1)
RowAvg(k1) = mean(M(r(k1),M(r(k1),:)~=500));
ColAvg(k1) = mean(M(M(:,c(k1))~=500,c(k1)));
end
Here, every ‘[r,c]’ pair defines a specific value for ‘RowAvg’ and ‘ColAvg’.
This works if there is only one ‘500’ in any specific row or column. If there are more than one ‘500’ in any row or column, you have to decide how you want to deal with the second ‘500’.
More Answers (1)
Thorsten
on 24 Aug 2016
Edited: Thorsten
on 24 Aug 2016
A = [2 5 3 500 4 5; 3 4 2 2 3 5; 4 5 5 1 2 2];
[i, j] = ind2sub(size(A), find(A==500));
m1 = mean(A(i, ~ismember(1:size(A,2), j)));
m2 = mean(A(~ismember(1:size(A,1), i), j));
A(i,j) = (m1 + m2)/2;
or
idx = A == 500
A(idx) = nan;
A(idx) = mean([nanmean(A(:, any(idx, 1))) nanmean(A(any(idx, 2), :), 2)])
2 Comments
See Also
Categories
Find more on Data Preprocessing 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!