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 个评论
abdul wahab  aziz
abdul wahab aziz 2016-8-24
i will search for 500 first then will replace that 500 with that row and column avg....here i need row avg where 500 exists and its column average but 500 must be excluded while calculating averages or mean
abdul wahab  aziz
abdul wahab aziz 2016-8-24
my finals ans would i will replace row_Avg+col_avg/2 with 500,but before that i have to calculat row_Avg and col_avg seperately of that point where 500 exists

请先登录,再进行评论。

采纳的回答

Star Strider
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
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
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 个评论
abdul wahab  aziz
abdul wahab aziz 2016-8-24
for i = 1:size(G2,1)
for j = 1:size(G2,2)
hidden=G2(i,j);
if(hidden==100)
K=K+1;
RowAvg=mean(G1(i, ~ismember(1:size(G1,2), j)));
ColAvg=mean(G1(~ismember(1:size(G1,1), i), j));
this is my code i also want to ignore zeroes, while calculating mean? can u add that thing in to it

请先登录,再进行评论。

Community Treasure Hunt

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

Start Hunting!

Translated by