HOW TO TAKE AVERAGE OF ROW AND COLUMN AT SOME POINT
조회 수: 2 (최근 30일)
이전 댓글 표시
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
채택된 답변
Star Strider
2016년 8월 24일
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
Star Strider
2016년 8월 24일
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’.
추가 답변 (1개)
Thorsten
2016년 8월 24일
편집: Thorsten
2016년 8월 24일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!