Masked array operations
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1613] Masked array operations
- From: msreeve at netcom.com (Mark D. Reeve)
- Date: Thu, 6 Jul 1995 22:12:55 -0400
- Organization: NETCOM On-line Communication Services (408 261-4700 guest)
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? Mark Reeve msreeve at netcom.com OR mreeve at xti.com