How to make combinations between all items of 2 lists in NetLogo 6.2?

Viewed 24

I have the following doubt: I have two lists with three values ​​each and I would like to make combinations between the items in these 2 lists.

For example: list1 [5 10 15] and list2 [2 4 8]. I would like to have the following combinations:

list3 [[5 2] [10 2] [15 2] [5 4] [10 4] [15 4] [5 8] [10 8] [15 8]]

I was only able to do the following combination:

let list1 ( list repro1 repro2 repro3 ) ;; input variable in the interface. repro1 = 5; repro2 = 10 and repro3 = 15
  let list2 ( list met1 met2 met3 ) ;; input variable in the interface. met1 = 2; met2 = 4 and met3 = 8
  let list3 ( map [ [ l1 l2 ] -> ( list l1 l2 ) ] list1 list2 )
  print list3 ;; command center: [[5 2] [10 4] [15 8]]

Can anybody help me?

Thanks in advance

1 Answers

Excellent work on the example code, thank you for doing that! You were definitely on the right track, you just needed to add an extra map inside the first so that the iteration would happen for each combination, rather than for item 0 in both lists, then item 1 in both lists, then for item 2 in both lists, etc:

to to combine
  let list1 [5 10 15] 
  let list2 [2 4 8]
  let list3 ( reduce sentence ( map [ i -> map [ j -> list i j ] list2 ] list1 ))
  print list3
end

Output:

[[5 2] [5 4] [5 8] [10 2] [10 4] [10 8] [15 2] [15 4] [15 8]]

reduce sentence above 'flattens' the list by one level so that you have a single list of lists, rather than a list of lists of lists.

If you want the exact order you indicated above, you can shuffle the order in which you pass the arguments- for example:

to combine
  let list1 [5 10 15] 
  let list2 [2 4 8]
  let list3 ( reduce sentence ( map [ i -> map [ j -> list j i ] list1 ] list2 ))
  print list3
end

Gives:

[[5 2] [10 2] [15 2] [5 4] [10 4] [15 4] [5 8] [10 8] [15 8]]

Not sure if the order of the items matters here, just wanted to point that out.

Related