I think what you may be missing is that boxed items can be used to index an array of dimensions higher than 1:
]rr=. 10 10?@$1000 590 147 158 729 522 732 355 5 955 673 858 350 740 105 669 546 334 840 546 184 629 608 375 697 224 240 921 182 477 509 463 692 151 192 639 115 543 589 636 769 240 916 823 902 529 591 757 343 755 622 589 665 484 430 849 430 460 987 769 628 731 581 107 42 23 145 195 820 223 487 725 362 263 376 945 883 760 897 675 188 89 884 484 790 793 178 131 324 291 199 527 52 725 187 867 297 411 495 985 478 ]whprm=. rr e. p: i.>./,rr NB. Which are prime? 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 1 0 0 0 1 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 Say we want to replace all primes by 1: whprm+rr*-.whprm NB. Arithmetical way 590 147 158 729 522 732 355 1 955 1 858 350 740 105 669 546 334 840 546 184 629 608 375 697 224 240 921 182 477 1 1 692 1 192 639 115 543 589 636 1 240 916 1 902 529 591 1 343 755 622 589 665 484 430 849 430 460 987 1 628 731 581 1 42 1 145 195 820 1 1 725 362 1 376 945 1 760 897 675 188 1 884 484 790 793 178 1 324 291 1 527 52 725 187 867 297 411 495 985 478 or, since ($ #: I.@:,) whprm 0 7 0 9 2 9 3 0 3 2 3 9 4 2 4 6 5 8 6 2 6 4 6 8 6 9 7 2 7 5 8 0 8 6 8 9 (1) (<"1 ($ #: I.@:,) whprm) } rr NB. Using indexes 590 147 158 729 522 732 355 1 955 1 858 350 740 105 669 546 334 840 546 184 629 608 375 697 224 240 921 182 477 1 1 692 1 192 639 115 543 589 636 1 240 916 1 902 529 591 1 343 755 622 589 665 484 430 849 430 460 987 1 628 731 581 1 42 1 145 195 820 1 1 725 362 1 376 945 1 760 897 675 188 1 884 484 790 793 178 1 324 291 1 527 52 725 187 867 297 411 495 985 478 On Sat, Oct 11, 2014 at 4:02 AM, Raul Miller <rauldmil...@gmail.com> wrote: > Yes: > > This mechanism, and the insight behind it, is the heart of the > multi-dimensional array mechanism. > > As a simple example, if you have a 10x10x10 array, and you want the value > at index 6,7,3 that value is at index 673 of the ravel of that array. > > It's about which representation is most convenient for the operation you > are performing. > > Thanks, > > -- > Raul > > On Sat, Oct 11, 2014 at 1:43 AM, Jon Hough <jgho...@outlook.com> wrote: > > > Also, regarding Ben Gorte's > > > > Idot =: $ #: I.@:, > > > > This is an equivalent of I. for higher dimensions. > > > > I'm having trouble understanding how this works. > > > > Here is my understanding. > > > > 1. ravel the matrix and index elements (I.@:,) > > 2. Next is a fork of $ #: I.@:, > > > > Right "prong" is the aforementioned element indices. > > > > Left "prong" is the shape of the original array/matrix. > > > > middle "prong" is the antibase of the right prong w.r.t. the left. > > > > This seems to work for matrices of any size or dimension. > > > > Is this the standard way to index multidimensional arrays? > > > > > > > > > From: jgho...@outlook.com > > > To: programm...@jsoftware.com > > > Date: Sat, 11 Oct 2014 06:33:45 +0100 > > > Subject: Re: [Jprogramming] Project Euler 85, Python and J > > > > > > Using (2 ! >:) is clearly better than doing my double for-loop. I'm > > embarrassed I missed that. > > > > > > The real meat of my confusion with multidimensional arrays is in not > > just finding the indices but doing something with the elements at the > > indices. > > > > > > e.g. For a single dimension array, there could be a function to fund > the > > primes less than 100. > > > > > > > > > primelist =: (I.@:(1&=)@:(1&p:)) { ] > > > > > > > > > and primelist >: i. 100 > > > > > > > > > should spit out > > > > > > > > > 2 3 5 ... ...97 > > > > > > > > > But what if instead of having >: i. 100 > > > > > > I had ( for some reason ) > > > > > > arr =: 10 10 $ >: i. 100 > > > > > > > > > So I have a 10 by 10 matrix of all positive ints up to 100. > > > > > > > > > primelist clearly will not work on arr. But if I want to return the > list > > of primes, as a single dimensional list I'm not sure how to do that. > > > > > > > > > Or, for example, if I want to change the elements of arr to 1 if and > > only if the sum of the (i, j) indices are prime (just a random example). > > > > > > > > > In procedural python this could be quickly done with a double for-loop > > and a prime test. In J this type of problem still escapes me. > > > > > > > > > > > > > Date: Fri, 10 Oct 2014 19:35:26 -0400 > > > > From: devon...@gmail.com > > > > To: programm...@jsoftware.com > > > > Subject: Re: [Jprogramming] Project Euler 85, Python and J > > > > > > > > countRects=: */@(2 ! >:) NB. How many pairs each of > > vertical > > > > * horizontal lines > > > > getSizes=: ,@(>:/~) # [: ,/ ,"0/~ NB. All pairs of i. y > > > > idxClosest=: 4 : '(i. <./)@(x |@:- ])y'"(0 2) NB. Index of mat y > > to > > > > value x > > > > ({~ *2e6*&idxClosest@:(countRects"1)) getSizes >: i.200 NB. > > Closest > > > > to 2e6 > > > > 77 36 > > > > ({~ *1e6*&idxClosest@:(countRects"1)) getSizes >: i.200 NB. > > Closest > > > > to 1e6 > > > > 63 31 > > > > countRects"1 ] 63 31,:77 36 NB. How close is each? > > > > 999936 1999998 > > > > > > > > > > > > On Fri, Oct 10, 2014 at 1:14 PM, Linda Alvord < > lindaalv...@verizon.net > > > > > > > wrote: > > > > > > > > > What is the correct answerfor this problem? > > > > > > > > > > Linda > > > > > > > > > > -----Original Message----- > > > > > From: programming-boun...@forums.jsoftware.com > > > > > [mailto:programming-boun...@forums.jsoftware.com] On Behalf Of > > Stefano > > > > > Lanzavecchia > > > > > Sent: Friday, October 10, 2014 11:47 AM > > > > > To: programm...@jsoftware.com > > > > > Subject: Re: [Jprogramming] Project Euler 85, Python and J > > > > > > > > > > Actuary the use of ravel and antibase is common practice to solve > > > > > certain problems in APL and isn't considered cheating. So I > wouldn't > > > > > say it's "not nice" but I would definitely go for antibase instead > of > > > > > a combination of floored-divide and modulus. As a bonus, a solution > > > > > based on antibase would scale to problems of any rank and not just > 2d > > > > > matrices. > > > > > > > > > > Have fun! > > > > > -- > > > > > Stefano > > > > > > > > > > > On 10/ott/2014, at 17:35, Sebastiano Tronto < > > sebastiano.tro...@gmail.com > > > > > > > > > > > wrote: > > > > > > > > > > > > Hi, > > > > > > A dirty trick to get the job done would be to ravel the matrix ( > , > > ), > > > > > solve > > > > > > the 1d version of the problem and then get the "true" indexes > with > > > > > > something like (<.@%&200 , 200&|). > > > > > > For example, if you needed to just find the max: > > > > > > (<.@%&200 , 200&|) (i. >./) , m > > > > > > where m is your matrix. > > > > > > > > > > > > I know this isn't a nice way to solve the problem, but it should > > work. > > > > > > > > > > > > Sebastiano > > > > > > > > > > > > 2014-10-07 6:37 GMT+02:00 Jon Hough <jgho...@outlook.com>: > > > > > > > > > > > >> Project Euler 85: https://projecteuler.net/problem=85 > > > > > >> This problem is not really conceptually hard, but I am > struggling > > with a > > > > > J > > > > > >> solution.I have solved it in Python: > > > > > >> ============================================= > > > > > >> def pe85(larg, rarg): count = 0 llist = range(1, larg+1) > > > > > >> rlist = range(1, rarg+1) > > > > > >> for l in llist: for r in rlist: > > count += > > > > > >> l*r > > > > > >> return count > > > > > >> > > > > > >> if __name__ == "__main__": # test for 2x3 grid, as in > > question. > > > > > k > > > > > >> = pe85(2,3) print "Test value: "+str(k) l1 = > > range(1,200) > > > > > # > > > > > >> 200 lucky guess l2 = range(1,200) bestfit = 10000 # > > just a big > > > > > >> number area = 0 for i in l1: for j in l2: > > > > > >> diff = abs(2000000 - pe85(i,j)) > > if diff > > > > > < > > > > > >> bestfit: area = i*j > > > > > >> bestfit = diff > > > > > >> print "AREA is "+str(area) > > > > > >> > > > > > >> > > > > > >> ================================================The above script > > will > > > > > give > > > > > >> the final area of the closest fit to 2 million. (The python code > > may not > > > > > be > > > > > >> the best). Also I tested all possibilities up to 200x200, which > > was > > > > > chosen > > > > > >> arbitrarily(~ish). > > > > > >> Next my J. I go the inner calculation ok (i.e. see the function > > pe85 > > > > > >> above). In J I have: > > > > > >> pe85 =: +/@:+/@:((>:@:i.@:[) *"(0 _) (>:@:i.@:])) > > > > > >> NB. I know, too brackety. Any tips for improvement appreciated. > > > > > >> > > > > > >> > > > > > >> But from here things get tricky. If I do the calculation over > > 200x200 > > > > > >> possibilities I end up with a big matrix, of which I have to > find > > the > > > > > >> closest value to 2 million, of which then I have to somehow get > > the > > > > > (x,y) > > > > > >> values of and then find the area by x*y. > > > > > >> > > > > > >> The main issue is getting the (x,y) from the best fit value of > the > > > > > array. > > > > > >> > > > > > >> i.e. If I do pe85"(0)/~ 200, I get a big array, and I know I can > > get the > > > > > >> closest absolute value to 2 million but then I need to get the > > original > > > > > >> values to multiply together to give the best fit area. Actually > I > > have > > > > > >> bumped into this issue many times. It is easy enough in a 1-d > > array,just > > > > > do: > > > > > >> (I. somefunc ) { ]) > > > > > >> > > > > > >> or similar to get the index. But for two indices the problem is > > beyond > > > > > me > > > > > >> at the moment. Any help appreciated.Regards,Jon > > > > > >> > > > > > >> > > > > > >> > > > > > >> > > ---------------------------------------------------------------------- > > > > > >> 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 > > > > > > > > > > > > > > > > > > > > > -- > > > > Devon McCormick, CFA > > > > > ---------------------------------------------------------------------- > > > > 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 > -- Devon McCormick, CFA ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm