MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: How to delete a row and a column in a matrix?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg69720] Re: How to delete a row and a column in a matrix?
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Thu, 21 Sep 2006 07:31:11 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <eeqqlr$onu$1@smc.vnet.net>

hussain.alqahtani at gmail.com wrote:
> Dear
> 
> I am wondering how to delete a given row and column in a matrix in a
> single command.?
> 
> Your prompt reply is highly appreciated.
> 

I believe that you are already aware of the Delete function. I am not 
aware of a similar function that allow simultaneously the deletion of 
rows and columns, so I wrote the following function that -- I hope -- 
should be self-explanatory and do what you are looking for.

In[1]:=
deleteRC[(mat_)?MatrixQ, row_Integer, col_Integer] :=
   Module[{m = mat, r = row, c = col},
    If[r != 0, m = Delete[m, r]];
     If[c != 0, m = Transpose[Delete[Transpose[m],
         c]]]; m]

In[2]:=
TableForm[m = {{a, b, c}, {d, e, f}, {j, h, k}}]

Out[2]//TableForm=
a   b   c

d   e   f

j   h   k

In[3]:=
TableForm[deleteRC[m, 1, 3]]

Out[3]//TableForm=
d   e

j   h

In[4]:=
TableForm[deleteRC[m, 0, 3]]

Out[4]//TableForm=
a   b

d   e

j   h

Regards,
Jean-Marc


  • Prev by Date: Re: Split a file into multiple files using a pattern
  • Next by Date: does the following code shut down anyone else's kernel? - why?
  • Previous by thread: Re: How to delete a row and a column in a matrix?
  • Next by thread: could someone explain Mathematica's protests about division by zero here?