how to do replacing-item in use nested list - netlogo

replacing in nested list ??
for example
[[1 2][3 4] [5 1]]
to
[[99 2][3 4] [5 1]]
Can anybody please clarify the use of lists in Netlogo?

Your question is somewhat underspecified. How do you want to decide which item gets changed?
I'll suppose that you want to do it by indices, e.g. "0th item of 0th sublist". Then that's:
to-report replace-subitem [index1 index2 lists value]
let old-sublist item index1 lists
report replace-item index1 lists (replace-item index2 old-sublist value)
end
observer> show replace-subitem 0 0 [[1 2] [3 4] [5 1]] 99
observer: [[99 2] [3 4] [5 1]]
You could also imagine doing the replacement according to other criteria. For example, suppose we want to change all of the occurrences of 1 in the first item of a sublist to 99. Then that's:
to-report replace-first [old new the-list]
if first the-list = old
[ report replace-item 0 the-list new ]
report the-list
end
to-report replace-firsts [old new lists]
report map [replace-first old new ?] lists
end
observer> show replace-firsts 1 99 [[1 2] [3 4] [1 1]]
observer: [[99 2] [3 4] [99 1]]
A lot of other answers are possible as well, depending on exactly what problem you're trying to solve.

Related

Netlogo: Sum a list of lists of equal length element-wise

I have a list of lists of equal lengths, e.g.
[[0 1 0] [2 3 0] [4 4 2] [0 1 0]]
How can I get the list [6 9 2] which sums up the entries in the four lists entrywise?
let _lst [[0 1 0] [2 3 0] [4 4 2] [0 1 0]] show reduce [[?1 ?2] -> (map + ?1 ?2)] _lst
As a procedure:
to-report aggregate-lists [list-of-lists]
report reduce [[?1 ?2] -> (map + ?1 ?2)] list-of-lists
end
Someone who is better at lists will come along and do this more cleanly, but this works.
to testme
let inlist [[0 1 0] [2 3 0] [4 4 2] [0 1 0]]
let outlist []
let ii 0
while [ii < length item 1 inlist ]
[ let items map [ x -> item ii x ] inlist
print items
set outlist lput reduce [ [a b] -> a + b] items outlist
set ii ii + 1
]
print outlist
end
What it does it create a list of the first entries (with let items) and then sums them with the reduce, then moves to the second entries etc.
You may also want to look at this question Netlogo: How to compute sum of items of lists within a list?, which works with individual entries.
I was looking for a "one-liner" and finally produced it:
to-report aggregate-lists [list-of-lists]
report map [ i -> sum (map [li -> item i li] list-of-lists) ] range length item 0 list-of-lists
end
You map a reporter which sums item i of all lists to the vector of indices range length item 0 list-of-lists (would be [0 1 2] in the example).

How to remove several items from a unsorted list in Netlogo

so i'm a bit struggling with Lists in Netlogo, so basically i've two lists and i want to remove the items that are in List 1 from List 2, for example:
List 1 : [8 6 9 7 1 3]
List 2: [5 9 8]
Resulting List : [6 7 1 3]
I've tried the following code but it returns an empty list:
if List 2 != []
[
foreach List 2
[
let p position ? List 1
if p = true
[
set List 1 remove-item p List 1
]
]
]
Any ideas ?
A combination of member? and filter will get you there:
let list1 [8 6 9 7 1 3]
let list2 [5 9 8]
let result filter [ x -> not member? x list2 ] list1
print result
Will print the desired:
[6 7 1 3]
Tip: whenever you find yourself trying to use an index for anything in NetLogo, you are probably not doing things in the optimal way. NetLogo has tons of functions (like filter, in this case) that operate on lists as a whole. There is rarely a need to explicitly loop through them.

How can I access elements of a nested list in netlogo

If there is a list such as [[1 2] [3 4] [4 5] ...] which is a list of turtle co-ordinates then how can I access the second element(ordinate) in netlogo
i.e for example I want to access '4' of the second tuple ([3 4])
Using item once on your list of lists will report a list. Using it again on that list will report a number:
observer> show item 1 [[1 2] [3 4] [4 5]]
observer: [3 4]
observer> show item 1 item 1 [[1 2] [3 4] [4 5]]
observer: 4

Different combination of turtles in two sets in netlogo

I am struggling in making program in netlogo which produces the different combinations of turtles into two sets.
For instance; there are total 10 turtles present in a system [0 1 2 3 4 5 6 7 8 9] and I want to produce different combinations of those turtles into two sets. {[0 6 7 8] [1 2 3 4 5 9]}, {[2 3 6 8 9] [0 1 4 5 7]}....... So on.
Any help would be really appreciated.
This may be what you want. The below code removes duplicates by sorting the lists and removing duplicates (i.e it would remove [[1 2] [3]] and [[2 1] [3]] because it would sort the first list which would then be [1 2] which would cause for the match). Chop off an element and insert it into the left sublist and recurse.
You may imagine that each number in the list corresponds with a who. so you could do
let alist [who] of turtles
Let me know if you have any questions
to-report permutate-sublist [added-whos remaining-whos]
ifelse length remaining-whos = 1
[
report (list (list (sort added-whos) (sort remaining-whos)))
]
[
let result (list)
foreach (sort remaining-whos)
[
let x ?
let new-whos (sentence added-whos x)
let new-remaining-whos (remove x remaining-whos)
set result (sentence (list (list (sort new-whos) (sort new-remaining-whos))) result)
set result (remove-duplicates (sentence (permutate-sublist new-whos new-remaining-whos) result))
]
report result
]
end
This is what happens when you print out the results for
let alist (list 1 2 3 )
show permutate-sublist (list) alist
[[[2 3] [1]]
[[1 3] [2]]
[[3] [1 2]]
[[1 2] [3]]
[[2] [1 3]]
[[1] [2 3]]]

How to form subset form a set of numbers in Netlogo

Dear Netlogo community,
I am looking to generate subset of a set of numbers.
for example if a set is [1 2 3 4 5] then subset would be [1 2] [1 3] [1 4] [1 5] [ 1 2 3] [1 2 4]....... I know we can generate very easily by using bit manipulation in java. But I have no idea how to implement in Netlogo. I am doomed. Any help would be really appreciated. Thanks
This is easiest to solve using recursion:
to-report subsets [xs]
if empty? xs [ report [[]] ]
let recurse subsets butfirst xs
report sentence recurse
map [fput first xs ?] recurse
end
The basic idea is that if you want the subsets of [1 2 3], first you find all the subsets of [2 3]. All of them are themselves subsets of [1 2 3], plus if you stick the 1 on the front of each of them, the resulting lists are part of the answer too.
sample run:
observer> print subsets [1 2 3]
[[] [3] [2] [2 3] [1] [1 3] [1 2] [1 2 3]]