I am trying to ask Netlogo to generate a sequence of numbers with repeated elements, e.g.
[1 1 1 2 2 2 3 3 3]
I tried to use the n-values N [i -> i] syntax but it just gives a sequential list of numbers, 0 to N.
So far, I have tried using n-values primitive with sentence, e.g.
let mylist ( list sentence
n-values 3 [1] sentence
n-values 3 [2]
n-values 3 [3]
)
The problem is that this still returns a list of lists (i.e. [[1 1 1 2 2 2 3 3 3]]) and this causes problems for me later when trying to add this list into a matrix.
Thanks!
reduce sentence (map [x -> n-values 3 [x]] (range 1 4))
Related
I am wondering whether if its possible to iterate over n item using for loop (foreach), where the list is of length x.
For example, for a list of length 6 (x), I want to iterate the first 4 items (n) and add them to another list B.
lets say list-A is [ 7 8 12 11 5 6]
output required: list-B should be [7 8 12 11]
The code below adds all the items from list A to list B, since iteration goes through the entire list. I want to stop it at the 4th iteration. so only the first 4 items will be added.
set list-A [ 7 8 12 11 5 6]
set list-B []
let n 4
foreach list-A [
i ->
set list-B lput i list-B
]
NetLogo has sublist to extract one list from the middle of another. In your case it would look like:
to testme
let list-A [ 7 8 12 11 5 6]
let list-B sublist list-A 0 4
print list-B
end
I have a matrix m and I want to have a user defined function which allows me to control the elements that are multiplied by a value.
The function matrix:set-and-report looks promising but I'm not sure how to implement this for multiple elements.
For example, I would like to multiply the first 3 elements of the matrix by -1 to move from this:
let m matrix:from-row-list [1 2 3 4 5 6]
print m
to this:
let n matrix:from-row-list [-1 -2 -3 4 5 6]
With matrix:set-and-report you were indeed pretty close to a solution. Please check the example, I hope this is what you were looking for. The report function has matrix as an input. Than you specify the row, than the index were you want to start the multiplication, where to end it, and finally the multiplier.
Extensions [
matrix
]
to test
let m matrix:from-row-list [ [1 2 3 4 5 6] [1 2 3 4 5 6] ]
print (word "original matrix " m)
print (word "modified matrix " matrix-row-manipulation m 0 0 3 -1)
end
to-report matrix-row-manipulation [matrix row columen-index-start columen-index-end multiplier]
let index (range columen-index-start columen-index-end 1)
foreach index [ i ->
set matrix matrix:set-and-report matrix row i (matrix:get matrix row i * multiplier )
]
report matrix
end
This will return you:
observer> test
original matrix {{matrix: [ [ 1 2 3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}
modified matrix {{matrix: [ [ -1 -2 -3 4 5 6 ][ 1 2 3 4 5 6 ] ]}}
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.
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]]]
I have each patch containing a list of length 4. For exaample
state = [ 1 0 1 4 ]
I want to check how many patches have such unique lists, given the order of elements in the list matters.
Thus, for example in each of 4 patches with the following:
[1 2 4 1] [ 1 2 2 3] [1 3 2 2] [1 2 4 1]
The output should be 3.
This will do it:
length remove-duplicates [state] of patches