Skip, do you need to handle a more general case where there could be multiple
5’s in any row ? (Or can this not occur ?)
I. returns a list of indices where a 5 matches.
So if there is no 5 then you see an empty list.
However monadic I. Is rank 1 (“1) by default, so the empty list is padded with
0.
To avoid padding you need to think of the rows as lists, so you can “see” an
empty list … like this perhaps:
<"1 ]5=m
┌───────┬───────┬───────┬───────┐
│0 0 0 0│0 0 0 1│1 0 0 0│0 0 1 0│
└───────┴───────┴───────┴───────┘
I. each <"1 ]5=m
┌┬─┬─┬─┐
││3│0│2│
└┴─┴─┴─┘
This structure handles 0, 1 or multiple instances per row (each cell is a list
of indices and is empty if not there).
You can detect if a row has an index by checking for 0<# of each list …
(0<#)each I. each <"1 ]5=m
┌─┬─┬─┬─┬─┐
│0│1│1│1│1│
└─┴─┴─┴─┴─┘
I think it is better to use the above approach to determine if present or not,
and where.
However if you wish the _1 as you wish, then you could do as follows using the
above data structure.
g=:((-@:(1 >. #)){.(_1,]))@:I.
g each <"1 ]5=m NB. Your case
┌──┬─┬─┬─┐
│_1│3│0│2│
└──┴─┴─┴─┘
g each <"1 ]5=m,3 5 5 6 NB. Or with multiple values in a row
┌──┬─┬─┬─┬───┐
│_1│3│0│2│1 2│
└──┴─┴─┴─┴───┘
Hope this helps, I’m sure g can be written more succinctly by others…/Rob
> On 14 Jan 2019, at 8:42 am, 'Skip Cave' via Programming
> <[email protected]> wrote:
>
> What I would really like is for I. to return a _1 whenever there is no 1 in
> the match array, since there cannot be a negative index:
>
> I.5=1 2 3 4 6 4 3 4 4 6 7 6
>
> 4 8 10
>
> Idot 5=1 2 3 4 6 4 3 4 4 6 7 6
>
> _1
>
>
> ]m=.|:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
>
> 1 2 3 4
>
> 2 3 4 5
>
> 5 4 3 2
>
> 2 3 5 4
>
> 5=m
>
> 0 0 0 0
>
> 0 0 0 1
>
> 1 0 0 0
>
> 0 0 1 0
>
> ,Idot .5=m
>
> _1 3 0 2
>
>
> Can a verb Idot be designed, that does this?
>
> Skip Cave
> Cave Consulting LLC
>
>
> On Sun, Jan 13, 2019 at 2:41 PM Henry Rich <[email protected]> wrote:
>
>> Right. Prefer (I.@:= ,) to I.@,@:= since it uses special code.
>>
>> Henry Rich
>>
>> On 1/13/2019 2:54 PM, 'Mike Day' via Programming wrote:
>>> You often see this sort of thing, returning pairs of indices of all
>> occurrences:
>>>
>>> 5 ($@] #.inv I.@,@:=) |: 1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
>>> 1 3
>>> 2 0
>>> 3 2
>>>
>>> You can obviously get the row indices using {:”1 or some such, and you
>> can of course make the bracketed code a named dyadic verb,
>>>
>>> Cheers,
>>>
>>> Mike
>>>
>>>
>>> Sent from my iPad
>>>
>>>> On 13 Jan 2019, at 17:55, 'Skip Cave' via Programming <
>> [email protected]> wrote:
>>>>
>>>> I know I can find the location (index) of a specific integer in a
>> vector of
>>>> integers using I.
>>>>
>>>> I.5=1 2 3 4 5 4 3 4 5 6 5 6
>>>>
>>>> 4 8 10
>>>>
>>>>
>>>> So I want to find the row index of a specific integer in an array of
>>>> integers:
>>>>
>>>> |:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
>>>>
>>>> 1 2 3 4
>>>>
>>>> 2 3 4 5
>>>>
>>>> 5 4 3 2
>>>>
>>>> 2 3 5 4
>>>>
>>>> 5=|:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
>>>>
>>>> 0 0 0 0
>>>>
>>>> 0 0 0 1
>>>>
>>>> 1 0 0 0
>>>>
>>>> 0 0 1 0
>>>>
>>>> ,I. 5=|:1 2 3 4,. 2 3 4 5,. 5 4 3 2 ,. 2 3 5 4
>>>>
>>>> 0 3 0 2
>>>>
>>>>
>>>> The first zero indicates that there is no 5 in the first row. The second
>>>> zero gives the index of the 5 in the third row. How can I tell whether
>> the
>>>> zero is an index, or a null indicator?
>>>>
>>>> Skip
>>>> ----------------------------------------------------------------------
>>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>> ----------------------------------------------------------------------
>>> For information about J forums see http://www.jsoftware.com/forums.htm
>>
>>
>> ---
>> This email has been checked for viruses by AVG.
>> https://www.avg.com
>>
>> ----------------------------------------------------------------------
>> 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