Mean of Nested Lists - netlogo

I'm trying to calculate the mean of nested lists. I have tried using the map function, but the default gives the mean in the opposite dimension that I am interested in. See the below example:
set a [[1 1][2 2][3 3]] ; create a nested list
set b map mean a ; b equals [1 2 3]
This answer gives [1 2 3] for b. However I am interested in the answer [2 2] by taking the mean in the "other" dimension. I would imagine there is a way to do this with map but haven't figured it out.

to go
print column-means [
[ 1 1 ]
[ 2 2 ]
[ 3 3 ]
]
end
to-report column-means [ matrix ]
if length (remove-duplicates map length matrix) > 1 [
error "All rows must be the same length"
]
report n-values length first matrix [ mean extract ? matrix ]
end
to-report extract [ i row ]
report map [ item i ? ] row
end

A possible solution is the following
set a [[1 2 3] [1 2 3]]
set b map mean a
this will give you [2 2] for b.

Related

Working with index positions on a list in Netlogo

I'm struggling with the following problem using these variables:
set variablex .5
set threshhold-list [0 .3 .6]
set variable-list [0 0 1]
I have three agenttypes 0,1,2 that correspond to the index position of threshhold-list and variable-list. So Agent 0 has threshold 0 and variable 0, Agent 1 has threshold .3 and variable 0, and Agent 2 has threshold .6 and variable 1.
What I'd like to do is check if any agent has a threshold greater than zero and less than variablex. If so, update that agent's variable on the variable list to variablex. That is, for the variables above I'd like to run logic that produces a new variable-list like this one:
variable-list [0 .5 1]
But if variablex was .7, it would produce [0 .7 .7].
I've got some code I've been hacking away at but I feel like it's way more complicated than the problem and so I'm wondering if someone could point me in the right direction. Thanks so much!
There are a few different ways to approach the problem, but if I was in your situation, I would first write a small reporter that gives me the value that should be stored at each index:
to-report new-value [ i ]
let t item i threshhold-list
report ifelse-value (t > 0 and t < variablex)
[ variablex ] ; the variable's new value should be variable x
[ item i variable-list ] ; the variable's value should not change
end
Once you have that, you can use either foreach or map to change your variable list:
to update-variables-with-foreach
foreach range length variable-list [ i ->
set variable-list replace-item i variable-list new-value i
]
end
to update-variables-with-map
set variable-list map new-value range length variable-list
end
Here is a somewhat verbose test to check that both versions would give you the expected results:
globals [
variablex
threshhold-list
variable-list
]
to test
clear-all
set threshhold-list [0 .3 .6]
set variablex .5
set variable-list [0 0 1]
update-variables-with-foreach
print variable-list
set variablex .5
set variable-list [0 0 1]
update-variables-with-map
print variable-list
set variablex .7
set variable-list [0 0 1]
update-variables-with-foreach
print variable-list
set variablex .7
set variable-list [0 0 1]
update-variables-with-map
print variable-list
end
That being said, as much as I think it is fun to play with lists, I think you are approaching your problem in a very unnetlogoish way.
NetLogo's world is a world of turtles and patches and links, not a world of arrays and indices and numbers.
You could do something along the lines of:
globals [
variable-x
]
turtles-own [
threshhold
variable
]
to setup
clear-all
set variable-x .5
(foreach [0 .3 .6] [0 0 1] [ [t v] ->
create-turtles 1 [
set threshhold t
set variable v
]
])
ask turtles [ update-variable ]
ask turtles [ show variable ]
end
to update-variable ; turtle procedure
if threshhold > 0 and threshhold < variable-x [
set variable variable-x
]
end
I don't know what you're ultimately trying to achieve, but if I could offer general advice, it would be to try to embrace to NetLogo mindset. Every time you're tempted to use an index of some kind in your code, take a step back and think again: there is probably a better (as in "more netlogoish") way to do it.

How to add a row to a matrix in NetLogo?

I have a 2*2 matrix called orders:
0 0
0 0
created by:
set orders matrix:from-row-list [[0 0] [0 0]]
and I would like to change it to be a 3*2 matrix:
0 0
0 0
10 50
How to do this please? (I guess you have to create a new matrix also called orders to overwrite the existing orders, but I couldn't figure out the syntax.)
The easiest way would probably be to first convert your matrix to a list, than add the new row to the list, and convert it back to a matrix. Not very elegant, but the report function below should do the trick:
extensions [ matrix ]
to-report matrix-add-row [matrix row-added]
let temp-list matrix:to-row-list matrix ;; converts the matrix to a list
set temp-list lput row-added temp-list ;; the new row is added to the list
report matrix:from-row-list temp-list ;; converts the list back to a matrix
end
to test
let orders matrix:from-row-list [[0 0] [0 0]]
show orders
show matrix-add-row orders [ 10 50 ]
end
This would return you:
observer> test
observer: {{matrix: [ [ 0 0 ][ 0 0 ] ]}}
observer: {{matrix: [ [ 0 0 ][ 0 0 ][ 10 50 ] ]}}
Of course, you have to make sure that the dimensions of the matrix and the row added match.
Just in case this helps someone else, what I did in the end was use a new agent breed 'orders' in place of the matrix, with each orders turtle essentially being what would have been a row in the matrix. I told the orders to sit on the same patch as the turtle that owned it, which was easy as the turtles in that model don't move. The advantage is that I had access to a wide range of processing possibilities that I didn't have with the matrix. Of course if you do this and the order of the rows matters, you need to include some way of managing this (something like orders-own [index] would do).

How to acces individual solutions of outputs in maple

I am trying to get to specific solutions from the output in maple. Sometimes there are multiple solutions and they come as tuples. If I assign a variable to the output, I would like to do something like x(1) gives me the first solution, x(2) gives me the second solution and so on.
with(LinearAlgebra):
with(VectorCalculus):
A := Matrix([[1, 2], [8, 1]])
x := Eigenvectors(A)
The eigenvectors x are:
Instead of using round brackets, use square brackets to denote the index position for the element you wish to return in the expression sequence. For example:
x[1];
x[2];
Returns:
Vector([-3, 5])
Matrix([[-1/2, 1/2], [1, 1]])
For more on indexing data structures in Maple, the 8th chapter in the user manual may come in handy.
The Eigenvectors command returns a sequence of two things.
So you could also use multiple assignment, to assign each to its own name directly. Eg,
with(LinearAlgebra):
with(VectorCalculus):
A := Matrix([[1, 2], [8, 1]]):
xvals, xvecs := Eigenvectors(A);
[-3] [-1/2 1/2]
xvals, xvecs := [ ], [ ]
[ 5] [ 1 1 ]
xvals;
[-3]
[ ]
[ 5]
xvecs;
[-1/2 1/2]
[ ]
[ 1 1 ]

Change some entries of a matrix in maple

I have a zero matrix in Maple and I need to replace some of its entries with some non zero elements. How do I do that? I also want to find the inverse of the new matrix after replacement.
If you created the Matrix by using the Matrix command then, by default, all its entries are zero. In that case you can subsequently assign to them as you wish, using indexed assignment.
M:=Matrix(2,2):
M;
[0 0]
[ ]
[0 0]
M[2,2]:=3.7:
M;
[0 0 ]
[ ]
[0 3.7]
The command to compute the inverse is LinearAlgebra:-MatrixInverse. Eg,
M:=Matrix(2,2):
M[2,2]:=3.7:
M[2,1]:=4.1:
M[1,2]:=-0.3:
M[1,1]:=5.9:
M;
[5.9 -0.3]
[ ]
[4.1 3.7 ]
Minv:=LinearAlgebra:-MatrixInverse(M):
Minv;
[0.160450997398092 0.0130095403295750]
[ ]
[-0.177797051170859 0.255854293148309 ]
M . Minv;
[1. 0.]
[ ]
[0. 1.]
See the help page for topic LinearAlgebra for more.

Sort nested list

I found this code to sort nested list.
to-report test
let z [[2 3] [1 9] [3 1] ]
report sort-by [(first ?1) < (first ?2)] z
end
What is first ?1 and first ?2 in this formula. It says that they are not defined as variables.
Are you perhaps using NetLogo v6.0? Your code works in v5.3.1, but v6.0 uses anonymous reporters. It would look like:
to-report test
let z [[2 3] [1 9] [3 1] ]
report sort-by [[list1 list2] -> first list1 < first list2] z
end
Charles