Discussion:
Deleting entire row and/or column
(too old to reply)
KFUPM
2009-04-01 19:50:12 UTC
Permalink
Dear all

I have large matrices having entire rows and/or columns of zeros and I
want to delete those zero rows and columns. My question, what is the
best (most compact) command to use in Mathematica. Please note that
the value of zero is the real zero i.e 0.0000 not just 0.

Regards,
Raffy
2009-04-02 09:47:08 UTC
Permalink
Post by KFUPM
Dear all
I have large matrices having entire rows and/or columns of zeros and I
want to delete those zero rows and columns. My question, what is the
best (most compact) command to use in Mathematica. Please note that
the value of zero is the real zero i.e 0.0000 not just 0.
Regards,
matKill0[m_?MatrixQ] :=
Part[m, Pick[***@Length[m], Equal[0, ##] & @@@ m, False],
Pick[***@Length@First[m], MapThread[Equal[0, ##] &, m], False]];
Ray Koopman
2009-04-02 09:47:18 UTC
Permalink
Post by KFUPM
Dear all
I have large matrices having entire rows and/or columns of zeros and I
want to delete those zero rows and columns. My question, what is the
best (most compact) command to use in Mathematica. Please note that
the value of zero is the real zero i.e 0.0000 not just 0.
Regards,
Leonid Shifrin
2009-04-02 09:49:16 UTC
Permalink
Hi,

I would suggest to introduce some cutoff epsilon, so that if the total of
any row
or column is less than epsilon, the row(column) is considered zero. This
will
speed up things considerably.

Consider a test "random" matrix:

In[1] =
testMatrix =
{{0.0000273478, 0.00003388, 17., 12.0001, 7.00002,
12.0001},
{0.0000192929, 0.0000107438, 0.0000910806, 0.0000370966,
0.000055372, 0.0000476642},
{0.0000649353, 0.0000388735, 17., 18.,
18., 18.},
{0.0000503573, 0.0000859998, 0.0000592802, 0.000086099,
0.0000915784, 8.01093*10^-6}};

(I made the small (zero) elements relatively large to avoid messy input). If
you set up
epsilon = 0.001, then rows 2,4 are zeros, as well as columns 1,2.

Here is then the code for the function that does what you want:

In[2] =

removeZeros[matr_, epsilon_] :=
Nest[***@Select[#, Total[Abs[#]] > epsilon &] &, matr, 2];

In[3] = removeZeros[testMatrix, 0.001]

Out[3] = {{17., 12.0001, 7.00002, 12.0001}, {17., 18., 18., 18.}}

If you need to delete only rows, use Select[#, Total[Abs[#]] > epsilon &]
&@matr
in the above definition, while if you need to delete only columns, use
Transpose[Select[#, Total[Abs[#]] > epsilon &] &@Transpose[matr]]

By making epsilon as small as you wish, you should be able to get what you
need.
At the same time, this solution will have a near linear complexity in the
maximal
matrix dimension (for the matrices not enormously large), while if you would
test element by element, you will end up with roughly quadratic complexity.

Regards,
Leonid
Post by KFUPM
Dear all
I have large matrices having entire rows and/or columns of zeros and I
want to delete those zero rows and columns. My question, what is the
best (most compact) command to use in Mathematica. Please note that
the value of zero is the real zero i.e 0.0000 not just 0.
Regards,
Raffy
2009-04-03 09:07:53 UTC
Permalink
Ah, I like the Nest/Transpose solution.
m***@gmail.com
2009-04-03 09:10:58 UTC
Permalink
Post by KFUPM
Dear all
I have large matrices having entire rows and/or columns of zeros and I
want to delete those zero rows and columns. My question, what is the
best (most compact) command to use in Mathematica. Please note that
the value of zero is the real zero i.e 0.0000 not just 0.
Regards,
yourMatrix /. x_ /;x == 0 -> Sequence[]

or if that doesn't work then

yourMatrix /. x : (_Real | _Integer) /; N[x] == 0. -> Sequence[]

you could change this to Chop[x] is the values ->0 could be considered
zero.

Mike
dh
2009-04-03 09:12:02 UTC
Permalink
Hi,

to delete rows with elements: 0 or 0. you may use:



DeleteCases[d, {a : (0. | 0) ..}]



To delet columns you use the above with transposeing the matrix.



Daniel
Post by KFUPM
Dear all
I have large matrices having entire rows and/or columns of zeros and I
want to delete those zero rows and columns. My question, what is the
best (most compact) command to use in Mathematica. Please note that
the value of zero is the real zero i.e 0.0000 not just 0.
Regards,
Raffy
2009-04-04 01:09:30 UTC
Permalink
Ah, I like the Nest/Transpose solution.
DrMajorBob
2009-04-04 01:13:51 UTC
Permalink
Really? I wish I could see it.

Bobby
Post by Raffy
Ah, I like the Nest/Transpose solution.
--
***@bigfoot.com
Raffy
2009-04-05 10:29:52 UTC
Permalink
Ah, I like the Nest/Transpose solution.
DrMajorBob
2009-04-05 10:34:18 UTC
Permalink
Really? I wish I could see it.

Bobby
Post by Raffy
Ah, I like the Nest/Transpose solution.
--
***@bigfoot.com
Continue reading on narkive:
Loading...