Filter löschen
Filter löschen

Taking Average of Nonzero Elements

1 Ansicht (letzte 30 Tage)
Imam
Imam am 17 Okt. 2014
Kommentiert: Imam am 17 Okt. 2014
Hi everyone,
I have got 2 questions, I hope someone can help me.
The first one is, I have a matrix A where
A = 1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
Then I apply the following
A = A>6 & A<11
[r,c,v] = find(A)
Doing that in the end I will have the information of indices that belong to those elements fulfilling the condition A>5 & A<12 in vectors r and c. Now I have another matrix B of the same dimensions as A.
B = 54 32 13 5
1 10 45 24
4 44 2 15
66 26 17 89
I want to choose only elements of B having the same indices as the elements in A that fulfilled the previous condition, leaving the other elements as zero. But if a row of B doesn't have such element matching what was mentioned above then it's truncated. So in the end I will have
result = 0 0 45 24
4 44 0 0
My last problem is simply how to take the average value on each element of matrix 'result' ignoring zeros, so what I will get is
mean = 34.5
24
I will really appreciate any help. Thanks in advance.

Akzeptierte Antwort

Matt J
Matt J am 17 Okt. 2014
Bearbeitet: Matt J am 17 Okt. 2014
L=(A>6 & A<11);
means=sum(B.*L,2)./sum(L,2);
means=means(~isnan(means))
  1 Kommentar
Imam
Imam am 17 Okt. 2014
Allright, it does help. Thanks a lot Matt.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Develop Apps Using App Designer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by