This is fairly concise, though not much more so than your function.
It doesn't check for
(a) whether the "squares" really are squares, since it assumes the 4th
columnis redundant,
(b) whether p lies in any square.
Also, (c) like yours, it only reports the first square.
Finally (d), I see that your 9 squares are minor squares in 0 0 150
150, and if you know of a higher structure, then you could possibly
exploit that knowledge more succinctly. For example, <.69 79 is 1 1
which may be regarded as indices in the square of minor squares.
Anyway, assuming you can't or don't want to use that insight,
finda=:(1 i.~ (((- 2&{.) (<:"1 */@:* 0 <: [) -/@(2 0 { ])) |:))
69 79 finda cor
4
1169 79 finda cor NB. doesn't check for no squares!
9
This variant deals with problems (b) & (c):
findb =: ((I.@: */@((- 2&{.) <"1 -/@:(2 0 { ]))) |:) NB. return
indices of ALL squares containing point
69 79 findb cor,60 65 100 105
4 9
1169 79 findb cor NB. return an empty list if point lies in none
of the squares
Finally, note that
50 50 findb cor NB. do you want squares to be closed intervals?
0 1 3 4
Mike
On 24/02/2013 7:54 AM, Gian Medri wrote:
Hi!
I have a matrix cor, where every row has the left upper and the right
bottom corner of a square.
cor
0 0 50 50
50 0 100 50
100 0 150 50
0 50 50 100
50 50 100 100
100 50 150 100
0 100 50 150
50 100 100 150
100 100 150 150
p=: 69 79
find=: 13 : '(*./"1 (x>"1 (2{."1 y))*.x< "1 (2}."1 y))# i. {.$y'
p find cor
4
I wonder if there is a more concise expression to find where the point p is.
Thanks
Gian Medri
----------------------------------------------------------------------
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm