MathGroup Archive 2008

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

Search the Archive

Re: function to check if array is empty


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