Iteration

Learning APL or new to Dyalog? Ask "silly" questions here, without fear...

Iteration

Postby StewKing on Tue Jul 05, 2011 5:48 am

Silly Q #2

I'm playing with random numbers. I generate many pairs of random numbers (random from 1 to 100 inclusive) and put them in a list. Then i have a matrix that's 100x100 that contains counts of how many times that pair has shown up. I've figured out how to do this successfully enough (i'm getting to like this language) except that i can't figure out how to iterate in an apl-like manner. If i were using a procedural language i would write something like:
Code: Select all
for i = 1 to number_of_rows_in_pairs_matrix
    count_matrix [pair_matrix[i,1], pair_matrix[i,2]] ++
next i

I have to believe that apl has a way to not have to loop on i in this manner. I'm really only interested in learning about the iteration part of this problem and not a better way to accumulate counts.

I have looked through the documentation but nothing jumps out at me except <each> but i couldn't figure out how to use that in this situation. Is this the right operator to use? Can apl do this kind of thing in a nice way? If so, where should i look for the syntax?

TIA.
-stew
StewKing
 
Posts: 4
Joined: Sun Jun 12, 2011 11:17 pm

Re: Iteration

Postby Phil Last on Tue Jul 05, 2011 10:00 pm

      ⍝ a small sample
z←10 10⍴0
z[?1000⍴⊂10 10]+←1
z
6 11 8 9 8 7 13 6 8 9
8 9 10 18 14 11 6 8 13 7
10 2 9 14 6 8 7 9 13 10
8 8 10 10 10 12 9 9 10 10
13 6 10 10 14 14 13 11 7 12
16 7 7 7 8 13 10 11 12 7
12 13 12 15 8 9 11 10 10 9
9 9 6 10 13 11 12 8 7 11
10 14 11 12 13 12 9 16 6 6
4 15 6 17 8 13 7 12 13 10
+/+/z
1000
User avatar
Phil Last
 
Posts: 628
Joined: Thu Jun 18, 2009 6:29 pm
Location: Wessex

Re: Iteration

Postby StewKing on Wed Jul 06, 2011 3:34 am

Thank you Phil, that will give me something to mull over.
StewKing
 
Posts: 4
Joined: Sun Jun 12, 2011 11:17 pm


Return to New to Dyalog?

Who is online

Users browsing this forum: No registered users and 1 guest