# How to extract a part of matrix satisfying certain condition(s)

I have a following matrix

matrix = {{1, 2, 3, 4, 5, 6, 7}, {2, 4, 6, 8, 10, 12, 14}, {3, 100, 9,
12, 15, 18, 21}, {4, 8, 12, 16, 20, 24, 28}, {5, 10, 15, 20, 100,
30, 35}, {8, 16, 24, 32, 40, 48, 100}, {9, 18, 27, 36, 45, 54,
63}};


I want to extract part of Matrix excluding rows that have 100 (I used 100 to represent missing observation in my data-set) as an element. I can get my results using the following codes:

result=Select[mat, #[[2]] != 100 && #[[5]] != 100 && #[[7]] != 100 &]


However, in my real data, I have a matrix with over 200 columns and I don't know the columns that have 100 as an element.

How can I obtain my results? If possible please let me know how I can get part of matrix excluding all columns that have 100 as an element. In this case my final matrix should be

For example, this removes both rows and columns containing 100

m = matrix[[Sequence @@ (Complement[Range@Length@matrix, #] & /@  Transpose@Position[matrix, 100])]];
m // MatrixForm


For removing rows containing 100 you could do (among others):

matrix /. {___, 100, ___} :> Sequence[]


or

DeleteCases[matrix, {___, 100, ___}]

• @rka Just transpose the matrix first .... Transpose@DeleteCases[Transpose@matrix, {___, 100, ___}] – Dr. belisarius Oct 26 '14 at 16:25
• @ belisarius, thank you for your answer. We can get the results for second part by transposing. Still, can you please suggest answer to second part of the question as well without transposing? I want to lean if possible. – ramesh Oct 26 '14 at 16:29

My favorite method being the one in @belisarius' answer using Part, or a slight variation of it, (matrix[[##&@@Complement@@@Transpose[{Range@Dimensions@matrix, Transpose@Position[matrix, 100]}]]]), here are a few more, clunkier, alternatives:

matrix//MatrixForm


pattern = Join @@ ({{#, _}, {_, #2}} & @@@ Position[matrix, 100]);
m2 = ReplacePart[matrix, Thread[pattern -> Sequence[]]] /. {} -> Sequence[]
m2 // MatrixForm


pattern2 = Join @@ ({{#, All}, {All, #2}} & @@@ Position[matrix, 100]);
m3 = matrix;
(m3[[##]] = Sequence[]) & @@@ pattern2;
m3 = m3 /. {} -> Sequence[];
m3 // MatrixForm
(* same result *)

• @ Kguler, Thanks for your answer. This gives some additional manipulation technique. – ramesh Oct 26 '14 at 16:20
• Your update is quite similar to my answer :) – Dr. belisarius Oct 26 '14 at 16:32
• @belisarius, indeed;) sorry, should have read more carefully. – kglr Oct 26 '14 at 16:37
• @kguler Nah, I find myself re-discovering answers all the time. :D – Dr. belisarius Oct 26 '14 at 16:48
Cases[Transpose @ matrix, a_List /; FreeQ[a, 100]] // Transpose // MatrixForm


• @ eldo, thanks for your answer to second part. – ramesh Oct 26 '14 at 16:19

You can use the Postion to exact the positions of 100

tagPos=Position[matrix, 100] (*{{3, 2}, {5, 5}, {6, 7}}*)


Then using the Last to achieve the column of 100

Rest/@Position[matrix, 100] (*{{2}, {5}, {7}}*)


So lastly, Delete

res=
Delete[Transpose@matrix, Rest /@ tagPos] // Transpose;
MatrixForm@res


$$\left( \begin{array}{cccc} 1 & 3 & 4 & 6 \\ 2 & 6 & 8 & 12 \\ 3 & 9 & 12 & 18 \\ 4 & 12 & 16 & 24 \\ 5 & 15 & 20 & 30 \\ 8 & 24 & 32 & 48 \\ 9 & 27 & 36 & 54 \end{array} \right)$$

### Or

If you hope to delete the row or column that owns 100, you can do like this:

res1 = Delete[Transpose@matrix, Rest /@ tagPos] // Transpose;
(*remove the column that containing 100*)
res2 = Delete[res1, Most /@ tagPos];
(*remove the row that containing 100*)
MatrixForm@res2


$$\left( \begin{array}{cccc} 1 & 3 & 4 & 6 \\ 2 & 6 & 8 & 12 \\ 4 & 12 & 16 & 24 \\ 9 & 27 & 36 & 54 \end{array} \right)$$

• @ ShutaoTang Thank for your reply. This gives me an additional method of doing my job. – ramesh Oct 27 '14 at 14:18
• @rka,my pleasure!:-) – xyz Oct 27 '14 at 14:25