6
$\begingroup$

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

enter image description here

Thank you in advance.

$\endgroup$
6
$\begingroup$

For example, this removes both rows and columns containing 100

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

Mathematica graphics

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

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

or

DeleteCases[matrix, {___, 100, ___}]
$\endgroup$
  • $\begingroup$ @rka Just transpose the matrix first .... Transpose@DeleteCases[Transpose@matrix, {___, 100, ___}] $\endgroup$ – Dr. belisarius Oct 26 '14 at 16:25
  • $\begingroup$ @ 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. $\endgroup$ – ramesh Oct 26 '14 at 16:29
4
$\begingroup$

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

enter image description here

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

enter image description here

pattern2 = Join @@ ({{#, All}, {All, #2}} & @@@ Position[matrix, 100]);
m3 = matrix;
(m3[[##]] = Sequence[]) & @@@ pattern2; 
m3 = m3 /. {} -> Sequence[]; 
m3 // MatrixForm
(* same result *)
$\endgroup$
  • $\begingroup$ @ Kguler, Thanks for your answer. This gives some additional manipulation technique. $\endgroup$ – ramesh Oct 26 '14 at 16:20
  • $\begingroup$ Your update is quite similar to my answer :) $\endgroup$ – Dr. belisarius Oct 26 '14 at 16:32
  • $\begingroup$ @belisarius, indeed;) sorry, should have read more carefully. $\endgroup$ – kglr Oct 26 '14 at 16:37
  • $\begingroup$ @kguler Nah, I find myself re-discovering answers all the time. :D $\endgroup$ – Dr. belisarius Oct 26 '14 at 16:48
2
$\begingroup$
Cases[Transpose @ matrix, a_List /; FreeQ[a, 100]] // Transpose // MatrixForm

enter image description here

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

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) $$

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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