MathGroup Archive 1995

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

Search the Archive

Re: Masked array operations

  • To: mathgroup at
  • Subject: [mg1667] Re: [mg1613] Masked array operations
  • From: Allan Hayes <hay at>
  • Date: Tue, 11 Jul 1995 04:37:15 -0400

"Mark D. Reeve" <msreeve at>
in [mg1613] Masked array operations

>Suppose I have a data array, say 1-D, and a boolean (1/0 or  
>True/False) array of the same length.  How I can I reduce the data
> array by the boolean array?
> e.g. 	data={1,2,3,4,5,6};
>    	 mask={1,0,0,1,0,1};
>I want something like the APL operator "/" such that mask/data
> yields {1,4,6}.  Ideally this operator should work over any
> arbitrary dimension of the array. ......
>You can do it with Select by first pairing the data and the mask,
> but this seems slow.  The best I have come up with is:
> ReduceX[arr_,mask_,dim_:1]:=
>	Module[{p},
>		p=Flatten[Position[mask,1],1];
>		Return[Map[#[[p]]&,arr,{dim-1}]];
>	];
> where dim is the dimension (first is 1) over which to perform the 
> reduction.  This seems rather unwieldy for such an outwardly simple 
> operation.  Can anyone improve it?


How about

ReduceX2[data_, mask_, levelspec_:1]:=
	Delete[data, Position[mask,0, levelspec]]



ReduceX2[data, mask]
	{1, 4, 6}
You can get more control by using more controls for Position

Allan Hayes
hay at

  • Prev by Date: Re: rhs of SetDelayed
  • Next by Date: Re: Options in self-defined functions
  • Previous by thread: Re: Masked array operations
  • Next by thread: Re: Labels on contourlines in ContourPlot[]