Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: function to check if array is empty

  • To: mathgroup at smc.vnet.net
  • Subject: [mg88329] Re: function to check if array is empty
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Fri, 2 May 2008 03:41:27 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • References: <fvbr6d$plc$1@smc.vnet.net>

will parr wrote:

> is there a function that will check if an array is empty? 
> 
> i'm looking for a function to return either True (or 1), or False (or 0) when it checks an array. eg: 
> 
> X = Array[0 &, {3, 3}]
> 
> then test with function (called ArrayEmpty, for example)
> 
> In: ArrayEmpty[X]
> 
> Out: True

If you deal only with m x n rectangular arrays (matrices) you could use

     ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]

A more general function would be

     ArrayEmptyQ[arr_List] :=
      If[Count[arr, x_ /; x != 0, -1] != 0, False, True]

For instance,

X = Array[0 &, {3, 3}]
ArrayEmptyQ[arr_List] := MatrixQ[arr, (# == 0 &)]
ArrayEmptyQ[X]
X[[1, 2]] = 1;
X
ArrayEmptyQ[X]

{{0, 0, 0}, {0, 0, 0}, {0, 0, 0}}

True

{{0, 1, 0}, {0, 0, 0}, {0, 0, 0}}

False

Regards,
-- Jean-Marc


  • Prev by Date: Wolfram Workbench user experiences
  • Next by Date: list of dates
  • Previous by thread: Re: function to check if array is empty
  • Next by thread: Re: Re: function to check if array is empty