Consider the following pre-generated table:
tab = Join[RandomReal[{0, 1}, {3, 2}], Table[{1}, 3],
RandomReal[{0, 1}, {3, 2}], Table[{0}, 3],
RandomReal[{0, 1}, {3, 2}], Table[{0}, 3],
RandomReal[{0, 1}, {3, 2}], Table[{-1}, 3], 2]
It consists of the groups of 3 columns. In each group, the third column has values 0, 1, -1. How to leave only the groups for which the third column takes the specific value? I.e., if I choose -1, then, schematically, the selected table would look like
tabsel=RandomReal[{0, 1}, {3, 2}], Table[{-1}, 3], 2]
This is my solution:
ProductsSelected[i_,val_] :=
If[tab[[1]][[3*(i - 1) + 3]] == val,
tab[[All, Range[3*(i - 1) + 1, 3*i]]], {}]
Join[ProductsSelected[1,1],ProductsSelected[2,1],
ProductsSelected[3,1], ProductsSelected[4,1],2]
However, the part with Join
is manual: I need to change it depending on the number of groups of columns.
Join@@Map[Cases[Partition[#,3],{_,_,-1}]&,tab]
on your real data and see if it works. $\endgroup$Join@@Map[Cases[Partition[#,3],{_,_,-1}]&,tab]
works correctly (at least on one test set and I see no reason for possible failure in general). My solution assumes that each row of tab is a multiple of 3 long. Bill's treats each row separately. This is a "salt to taste" situation. With my assumption,Join @@ Cases[Partition[Flatten[tab], 3], {_, _, -1}]
also works. $\endgroup$