2
$\begingroup$

Given,

data = {{1, 0, 1, 0, 1}, {2, 0, 2, 0, 2}, {3, 0, 3, 0, 3}, {4, 0, 4, 
    0, 4}, {5, 0, 5, 0, 5}};

Delete the columns whose sum totals to zero. Specifically, columns 2 and 4. I have tried creating a list of the sums:

list = Total[data, {1}];
{15, 0, 15, 0, 15}

and then summing through this list and deleting the cases in "data" where its total is zero. However, this is not working. If anyone could help me with this problem, I would appreciate it.

$\endgroup$
4
  • 3
    $\begingroup$ Transpose@Pick[Transpose@data, Map[Positive, list]] $\endgroup$
    – Domen
    Jul 19 at 21:24
  • 2
    $\begingroup$ Yoou could do Transpose[Select[Transpose[data], Total@# != 0 &]] (Edit: Domen beat me to it by 4 seconds!) $\endgroup$
    – Carl Lange
    Jul 19 at 21:24
  • $\begingroup$ Thank you, this worked! $\endgroup$ Jul 19 at 21:35
  • $\begingroup$ Transpose@data /. x_ /; Total[x] == 0 -> Nothing // Transpose $\endgroup$
    – Syed
    Sep 16 at 17:42

Your Answer

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

Browse other questions tagged or ask your own question.