필터 지우기
필터 지우기

HOW TO TAKE AVERAGE OF ROW AND COLUMN AT SOME POINT

조회 수: 2 (최근 30일)
abdul wahab  aziz
abdul wahab aziz 2016년 8월 24일
댓글: abdul wahab aziz 2016년 8월 24일
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
abdul wahab  aziz
abdul wahab aziz 2016년 8월 24일
what if 500 exists multiple time?
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일
THANK YOU THORSTEN YOUR ANSWER SOLVED MY PROBLEM
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