Roger's solution includes an intermediate-advanced J concept I knew about, and 
an advanced realization I did not previously realize/know about.

first, if you bind a verb (&) you normally make the result monadic.  If you 
call that verb dyadically, then result is:

(m&f)^:(x)  y

m&(] operand) gives the result of just monadic operand. 

The advanced realization is that compared to operator^:bitmask, you can 
evaluate the (^:n) "with rank" or for each n element.

An alternate solution that doesn't use the "advanced &" feature is:

bitmask  operator@]^:["0 operand

   0 1 0 1 >:@]^:[("0) 2 2 2 2

2 3 2 3


Another alternative that could work dyadically or with alternate verbs in J902,

(]`>: {~ 0 1 0 1)("0) 2 2 2 2

2 3 2 3

1 2 1 2 (-~`+ {~ 0 1 0 1)("0 0) 2 2 2 2

1 4 1 4






On Sunday, August 16, 2020, 04:12:05 p.m. EDT, Raoul Schorer 
<[email protected]> wrote: 





Thanks, works perfectly! Now I'll have a good think and try to understand
how this works ;-)

On Sun, Aug 16, 2020 at 10:02 PM Roger Hui <[email protected]>
wrote:

> If "operator" is >: there is a very easy solution:
>
>    desired_result -: operand + bitmask
> 1
>
> For a general monadic function, you can do as follows:
>
>    desired_result -: bitmask 0&(] operator)"0 operand
> 1
>
> The basic idea is what the power operator does; m&v or v&m allows you to do
> the same when the bitmask is an array and not a scalar.
>
>
>
>
> On Sun, Aug 16, 2020 at 12:45 PM Raoul Schorer <[email protected]>
> wrote:
>
> > Hi,
> >
> > This seems pretty trivial, but I'm stuck on how to perform conditional
> > application of a verb on a multidimensional array as defined by a bitmask
> > of the same shape as the operand array. Is there a typical idiom for
> that?
> >
> > For example,
> >
> > operand =. 2 2 2 $ 3 2 2 3 2 3 3 2
> >
> > bitmask =. 2 2 2 $ 1 0 0 1 0 1 1 0
> >
> > operator =. >:
> >
> > desired_result =. 2 2 2 $ 4 2 2 4 2 4 4 2
> >
> >
> > I tried looking at the doc, but I wasn't able to formulate a nice
> solution.
> >
> >
> > Thanks!
> >
> > Raoul
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm

> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to