5
$\begingroup$

I have:

list = {{1, 3, 4, 5, 6, 0, 9}, {4, 0, 3, 5, 0, 2, 0}, 
        {1, 0, 2, 2, 2, 4, 0}}

I want to calculate the average but without considering the 0s.

The result should be:

result = Array[0 &, Last@Dimensions@list];

n = Length@result;

Do[
  array = list[[All, i]];
  total = Total[array];
  cnt = Count[array, 0];

  If[Length@array - cnt > 0,
   result[[i]] = total/(Length@array - cnt),
   result[[i]] = 0
   ];,
  {i, 1, n}
  ];

result // N

{2., 3., 3., 4., 4., 3., 9.}

How can I replace the Do loop?

$\endgroup$
  • 1
    $\begingroup$ Mean /@ DeleteCases[Transpose@list, 0, All] $\endgroup$ – MarcoB Mar 8 '19 at 14:10
  • $\begingroup$ Thank you for the solution. $\endgroup$ – lio Mar 8 '19 at 14:12
  • $\begingroup$ You’re welcome. I’ve added an answer since this solution works for you $\endgroup$ – MarcoB Mar 8 '19 at 14:13
  • 5
    $\begingroup$ I'm not at a computer right now, so please try Total[list]/Total[Unitize[list]]. $\endgroup$ – J. M.'s technical difficulties Mar 8 '19 at 14:21
  • $\begingroup$ @J.M. Well, that is clever! Cool trick :-) $\endgroup$ – MarcoB Mar 8 '19 at 16:50
8
$\begingroup$

I'm posting this as a CW answer, so J.M.'s very nice answer, made in a comment above, gets recorded as a real answer.

list = {{1, 3, 4, 5, 6, 0, 9}, {4, 0, 3, 5, 0, 2, 0}, {1, 0, 2, 2, 2, 4, 0}};
Total[list]/Total[Unitize[list]]

{2, 3, 3, 4, 4, 3, 9}

Update

As J.M. points out in a comment below, there is more robust formulation that handles the cases where one or mort columns contain all zeros.

list = {{1, 3, 4, 5, 6, 0, 0}, {4, 0, 3, 5, 0, 2, 0}, {1, 0, 2, 2, 2, 4, 0}};
 Total[list]/(Total[Unitize[list]] /. 0 -> 1)
{2, 3, 3, 4, 4, 3, 0}
$\endgroup$
  • 1
    $\begingroup$ This produces an error for: list = {{1, 3, 4, 5, 6, 0, 0}, {4, 0, 3, 5, 0, 2, 0}, {1, 0, 2, 2, 2, 4, 0}} $\endgroup$ – lio Mar 11 '19 at 14:58
  • 3
    $\begingroup$ @lio, change the denominator to (Total[Unitize[list]] /. 0 -> 1). $\endgroup$ – J. M.'s technical difficulties Mar 11 '19 at 15:02
8
$\begingroup$
Mean /@ DeleteCases[Transpose@list, 0, All]

{2,3,3,4,4,3,9}

|improve this answer|||||
$\endgroup$
  • $\begingroup$ This produces an error for: list = {{1, 3, 4, 5, 6, 0, 0}, {4, 0, 3, 5, 0, 2, 0}, {1, 0, 2, 2, 2, 4, 0}} $\endgroup$ – lio Mar 11 '19 at 14:58
  • $\begingroup$ @lio, It does not produce an error. It just returns Mean[{}] in the case that all entries are zero. What result would you expect in that case? $\endgroup$ – MarcoB Mar 11 '19 at 16:09
  • 1
    $\begingroup$ I think the OP expects {2, 3, 3, 4, 4, 3, 0} in this case. $\endgroup$ – m_goldberg Mar 11 '19 at 18:20
1
$\begingroup$
ClearAll[means]
means = Mean[#["NonzeroValues"] /. {} -> {0}] & /@ SparseArray[Transpose[#]] &;

Examples:

list1 = {{1, 3, 4, 5, 6, 0, 9}, {4, 0, 3, 5, 0, 2, 0}, {1, 0, 2, 2, 2,  4, 0}};
means @ list1

{2, 3, 3, 4, 4, 3, 9}

list2 = {{1, 3, 4, 5, 6, 0, 0}, {4, 0, 3, 5, 0, 2, 0}, {1, 0, 2, 2, 2,  4, 0}};
means @ list2

{2, 3, 3, 4, 4, 3, 0}

|improve this answer|||||
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.