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

9 Answers 9

5
$\begingroup$
f1 = Transpose @* Select[UnequalTo[0] @* Tr] @* Transpose;

f1 @ data
{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}}
f2 = Transpose @* DeleteCases[_?(EqualTo[0] @* Tr)] @* Transpose;

f2 @ data
{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}}
$\endgroup$
1
  • $\begingroup$ particularly like f1 +1 :) $\endgroup$
    – ubpdqn
    Apr 18, 2022 at 8:41
3
$\begingroup$

And another fun one

Drop[data, 
  None, {##, 2} & @@ Flatten@Position[Total@data, 0]] // MatrixForm

res

$\endgroup$
3
$\begingroup$

Another possibility is to use SequenceCases:

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}};

enter image description here

Transpose[Map[If[SequenceCases[#, list_ /; Total[list] =!= 0] === {}, Nothing, #] &, Transpose[data]]]

enter image description here

$\endgroup$
3
$\begingroup$

Another approach is the following:

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}};

enter image description here

Transpose[Map[If[Total[data[[All, #]]] === 0, Nothing, data[[All, #]]] &, Range[Length[data]]]]

enter image description here

$\endgroup$
3
$\begingroup$

Another possibility is to use Table:

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}};

enter image description here

Transpose[Table[If[Total[data[[All, i]]] > 0, data[[All, i]], Nothing], {i, 1, Length[data]}]]

enter image description here

$\endgroup$
3
$\begingroup$
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}};

pos = Flatten@Position[Total[data], 0]

{2, 4}

data[[All, pos]] = Nothing

Result:

{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}, {4, 4, 4}, {5, 5, 5}}

$\endgroup$
1
  • $\begingroup$ Hi @Syed! Welcome to the party called by @Nasser :) $\endgroup$ Apr 18, 2022 at 8:12
2
$\begingroup$

We have

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}};

data // MatrixForm

mat1

  1. Use Position and Total to locate which columns you want removed and then Delete to delete them.

The code is:

Transpose@
  Delete[Transpose@data, Position[Total@data, 0]] // MatrixForm

mat2

  1. This is a minor comment. You can use Select and Total as was suggested in the comment but there's no need for Transpose.

The code is:

Select[Total@# != 0 &] /@ data // MatrixForm

mat2

$\endgroup$
2
$\begingroup$

Just for fun, another possibility is to use MapIndexed (there has to be at least 10 different ways to solve the same problem in Mathematica. I counted only 7 so far (including in comments). This forum needs to become more active :)

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}};

Mathematica graphics

sum = Total[data, {1}]
MapIndexed[If[#1 == 0, data[[All, First[#2]]] = Sequence[], Nothing] &, sum];

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Most of the times, I keep forgetting about our friend the MapIndexed. Not sure why. Nicely done!!! $\endgroup$
    – bmf
    Apr 18, 2022 at 0:16
  • $\begingroup$ @bmf the mapindexed is like the enumerate in python :). which will give you the element and index at the same time. $\endgroup$ Apr 18, 2022 at 6:53
  • $\begingroup$ @AsukaMinato not a python user, but thanks for letting me know. At some point I have to teach myself :) I am aware what MapIndexed does, but for some reason it will slip my mind most of the times...my dysfunctional brain :) $\endgroup$
    – bmf
    Apr 18, 2022 at 14:21
2
$\begingroup$

Since @Nasser wrote the infamous statement about the 10 ways, I cannot resist.

Drop[data, None, 
  Append[2]@Flatten@Position[Total@data, 0]] // MatrixForm

res

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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