You are here

Adding values into array

I need to store values of variables into a matrix or an array while the model is running and calculating these values, in other words I need to have something like
array.addElement(val)
function which will add elements on a fly. What would you suggest?

Forums: 

Hi Alex, not sure I have enough information here to help with your problem. Can you tell me what you're trying to do, with an example model?

--Jasper

Hi Jispert

I am working on a spatial analysis of water movement in a watershed.
I have tow matrices one-is an elevation map, and another one is water run off matrix. Theses matrices have same dimensions. My job is to calculate the total accumulation in each point. I search the spatial grid for smallest points, but add values from run off matrix. I need to create a total accumulation matrix on a fly by iterating on elevation map and adding values from run off matrix.
Moreover, at this point I am using random values for water run off matrix, but I will need to calculate these values from an elevation map, so again I will need to create matrix on a fly.

Elevation Map >> Water Run off Matrix >> Accumulation Map

Alex

Well you have the last(x) function to refer to an expression's values on the last time step, and the prev(n) function to refer to the value of the component you are evaluating n timesteps ago.

You also have prev(0) to refer to your component's value immediately prior to the current operation, but this is only useful in an 'alarm' iteration loop or when iterating across an array. When using it you must be careful to set a boundary condition in this so it has a value on initialization.

All these functions work on arrays, which can be operated on with the same functions as scalars.

--Jasper

I am not sure if that will work.
Let me restate.
Let say I have an initial array of four values [0,0,0,0]
Now in each time step I need to change one of the values [0,2,0,0] and again, [0,2,0,12], and so on until all values are changed.
I know the dimension of my array, but I will need to dynamically change values in it, how can I do that? I use a time step for my iterations.

OK, let's see if I've got this. You start with an array of zeros, and each time step increment one of the values, whose index is given by another variable. Suppose your increment is n, and the index of it is x.

First off, you need to make an array whose only nonzero value is n at position x. To make an array you use makearray(a,b) where a is the value to put in the array elements and b is the size of the elements. Inside the subexpression a you can use place_in(1) to refer to the index of that element.

So to make your array, supposing it has 4 elements, you write

makearray(if place_in(1)==x then n else 0, 4)

With x of 2 and n of 12 this will give

[0, 12, 0, 0]

Now you need to start off with another array that is all zeros and add this array each time step. You don't need to explicitly make an array of all the zeros because Simile will know it is an array from the fact that you are adding an array to it. So if you call the above array 'increment', you could specify your totals as follows:

if time()==0 then 0 else prev(1)+[increment]

here, prev(1) refers to the value of the whole array on the previous time step.

Hope this does what you wanted
--Jasper

I do not use indexes. All I have is an array of a fixed size, and I want to add new elements at different positions. Positions and values will be determined by my other sub programs.
Example:
I know that my array is five elements long [0,0,0,0,0]
Now I need to add some calculated number at some random position between 1 and 5
Number is 6, position 5
[0,0,0,0,6]
Next iteration
Number is 4, position 2
[0,4,0,0,6]
Next
Number is 8, position 1
[8,4,0,0,6]
And so on

As you see the previous numbers stay there unless I assign a new value to the same position
Number is 1, position 5
[8,4,0,0,1]

I need a flexibility to write numbers at any position of my array, including overrides
I do not need to increase the dimension of the array beyond its initial size

So far I was able to read numbers from arrays using place_in() and makearray() operators, but I did not see an operator which would allow me to write values back into array after the array was created.
Please help

Alex

I am sorry,
I just have tested the last suggestion with
makearray(if place_in(1)==x then n else 0, 4) and
if time()==0 then 0 else prev(1)+[increment]
formulas and it did work
Thank you vary much
The only problem is that I need to be able to override values with same postions, but these formulas ads values instead
Any suggestions about that?
Thank you again.
I was confused with the mention of indexes in the last suggestion that why I thought it would not work. I don’t use multiple instances of sub models and indexes associated with them; I use multi dimensional arrays and time() function for all my models.

Alex

I see. If you want to overwrite the previous value, you should do it all in one step, and give your array this formula

makearray(if place_in(1)==n then x else element(prev(1),place_in(1)), 5)
where:
n is the position to insert your new value
x is the value to insert
5 is the size of the array
--Jasper

Thank you for your help

Alex