# [✓] Pick columns or rows of a matrix?

Posted 1 year ago
1596 Views
|
9 Replies
|
10 Total Likes
|
 I work with random matrices made of 0 and 1. Once generated, I want to remove the columns that contains only zero's. I can easily identify these columns (as a list), and I can remove one column at a time. How to do it in one operation ? Here is how the nb starts: m = 100; n = 10 A = RandomInteger[1, {m, n}] Position [A, Table[0, n]] ...An alternative would be to exclude from the start columns with only zero's. Thanks for suggestions. Pierre Dehez, University of Louvain, Louvain-la-Neuve
9 Replies
Sort By:
Posted 1 year ago
 You can do the detection and removal at once: m = 100; n = 4 A = RandomInteger[1, {m, n}] Count[A, {0, 0, 0, 0}] A = A /. {0..} :> Nothing Count[A, {0, 0, 0, 0}] Basically, it replaces rows that are made up of only repetitions of 0 with Nothing (if you have an older version substitute Nothing with Sequence[])
Posted 1 year ago
 Thanks! Elegant. Pierre
Posted 1 year ago
 You could also use: m = 100; n = 4 A = RandomInteger[1, {m, n}] Position[A, Table[0, n]] Delete[A, %] Delete can accept multiple 'deletions' at the same time…
Posted 1 year ago
 Even simpler, thanks! I had overlooked the Delete command.
Posted 1 year ago
 The function DeleteCases could be used. Create a matrix where 0 is twice as likely to occur as 1: In[1]:= matrix = RandomChoice[{50, 25} -> {0, 1}, {20, 3}] Out[1]= {{1, 0, 0}, {0, 0, 0}, {0, 0, 1}, {1, 1, 1}, {0, 0, 0}, {1, 0, 0}, {1, 0, 1}, {0, 0, 1}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0, 1}, {0, 1, 1}, {1, 0, 0}, {1, 1, 0}, {1, 0, 0}, {0, 1, 0}} In[2]:= Length@matrix Out[2]= 20 Then DeleteCases could remove the rows that does not have at least one nonzero element: In[3]:= matrixCleanedOfAllZeroRows = DeleteCases[matrix, {0 ..}] Out[3]= {{1, 0, 0}, {0, 0, 1}, {1, 1, 1}, {1, 0, 0}, {1, 0, 1}, {0, 0,1}, {0, 1, 0}, {0, 0, 1}, {0, 1, 1}, {1, 0, 0}, {1, 1, 0}, {1, 0, 0}, {0, 1, 0}} In[4]:= Length@matrixCleanedOfAllZeroRows Out[4]= 13
Posted 1 year ago
 Great also because you suggest a way to produce matrices with few 1's, what I needed.
Posted 1 year ago
 To sum up, there are at least a dozen ways of doing it, and you can come up with a couple dozen more if one wants… Select[A, Positive@*Total] Select[A, AnyTrue@Positive] Select[A, Not@*AllTrue[PossibleZeroQ]] A /. {0 ..} :> Nothing A /. {0 ..} :> Sequence[] Delete[A, Position[A, Table[0, n]]] Delete[A, Position[A, {0 ..}]] DeleteCases[A, {0 ..}] Cases[A, Except[{0 ..}]] Select[A, MemberQ[1]] Pick[A, Positive@*Total /@ A, True] Pick[A, NoneTrue[Positive] /@ A, False] Reap[Scan[If[MemberQ[#, 1], Sow[#]] &, A]][[2, 1]] Reap[If[MemberQ[#, 1], Sow[#]] & /@ A][[2, 1]] good luck studying them…