Azzera filtri
Azzera filtri

HOW TO TAKE AVERAGE OF ROW AND COLUMN AT SOME POINT

2 visualizzazioni (ultimi 30 giorni)
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 Commenti
abdul wahab  aziz
abdul wahab aziz il 24 Ago 2016
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 il 24 Ago 2016
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

Accedi per commentare.

Risposta accettata

Star Strider
Star Strider il 24 Ago 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 Commenti
Star Strider
Star Strider il 24 Ago 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’.

Accedi per commentare.

Più risposte (1)

Thorsten
Thorsten il 24 Ago 2016
Modificato: Thorsten il 24 Ago 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 Commenti
abdul wahab  aziz
abdul wahab aziz il 24 Ago 2016
THANK YOU THORSTEN YOUR ANSWER SOLVED MY PROBLEM
abdul wahab  aziz
abdul wahab aziz il 24 Ago 2016
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

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by