ThomasC
ThomasC

Reputation: 881

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.)

Upvotes: 0

Views: 277

Answers (2)

ThomasC
ThomasC

Reputation: 881

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).

Upvotes: 0

geruter
geruter

Reputation: 386

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.

Upvotes: 2

Related Questions