1
$\begingroup$

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.

$\endgroup$
2
  • 2
    $\begingroup$ Try Join@@Map[Cases[Partition[#,3],{_,_,-1}]&,tab] on your real data and see if it works. $\endgroup$
    – Bill
    May 6 at 22:59
  • 1
    $\begingroup$ 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$
    – anon
    May 7 at 15:04

2 Answers 2

3
$\begingroup$

I admit that I changed your data so that the $-1$s would not be in predictable positions or quantities and I increased the number of rows to have a better chance of having at least some $-1$s.

n = 6
tab = ArrayFlatten[{{
    RandomReal[{0, 1}, {n, 2}], RandomInteger[{-1, 1}, {n, 1}], 
    RandomReal[{0, 1}, {n, 2}], RandomInteger[{-1, 1}, {n, 1}], 
    RandomReal[{0, 1}, {n, 2}], RandomInteger[{-1, 1}, {n, 1}], 
    RandomReal[{0, 1}, {n, 2}], RandomInteger[{-1, 1}, {n, 1}]}}, 2]; 
MatrixForm[tab]
MatrixForm[Select[Partition[Flatten[tab], 3], #1[[3]] == -1 & ]]
$\endgroup$
2
$\begingroup$
SeedRandom[1];
(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]) // TableForm

tab // Grid

$$\begin{array}{cccccccccccc} 0.817389 & 0.11142 & 1 & 0.542247 & 0.231155 & 0 & 0.422851 & 0.247495 & 0 & 0.29287 & 0.208051 & -1 \\ 0.789526 & 0.187803 & 1 & 0.396006 & 0.700474 & 0 & 0.977172 & 0.825163 & 0 & 0.580474 & 0.128821 & -1 \\ 0.241361 & 0.0657388 & 1 & 0.211826 & 0.748657 & 0 & 0.925275 & 0.578056 & 0 & 0.306427 & 0.712012 & -1 \\ \end{array}$$

To remove the 3 columnns with -1 as their last element:

(res = Partition[#, 3] & /@ tab // DeleteCases[#, {_, _, -1}, {2}] & //
     Map[Flatten]) // Grid

$$\begin{array}{ccccccccc} 0.817389 & 0.11142 & 1 & 0.542247 & 0.231155 & 0 & 0.422851 & 0.247495 & 0 \\ 0.789526 & 0.187803 & 1 & 0.396006 & 0.700474 & 0 & 0.977172 & 0.825163 & 0 \\ 0.241361 & 0.0657388 & 1 & 0.211826 & 0.748657 & 0 & 0.925275 & 0.578056 & 0 \\ \end{array}$$

$\endgroup$

Your Answer

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

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