You are here

Regarding The Double Loop

I was able to implement double loop search for matrix
However, there last problem that I have encountered, circular loop
I load a sample Matrix into my 1st sub which creates a random 3x3 matrix (borders are taken care of) --That Works
I pass 3x3 Matrix to Searching Sub (name: 3x3), which finds the smallest value and gives me Row position, Column position and Value--That Works
Now I need to pass Column and Row position of smallest value to a Searching Sub again to search for smallest cells around it-- Does Not Work, tells me that it contains a Circular functions, and it does, but I need to search entire matrix so it has to search for smallest value then reassign the coordinates and search again, until it reaches the border or smallest value.
I am including Equation List, The problem is with Sub final_3x3
What Can I do?

quations in Matr_Search2

Variable matr_sample
matr_sample = [[_x3_matr]]
Where:
[[_x3_matr]]=final_3x3/3x3_matr

Variable nxn_matr
nxn_matr = [[5,3,2,4],[6,3,5,7],[2,8,9,6],[3,1,5,7],[7,8,9,5]]

Equations in 3x3

Variable col_pos
col_pos = posleast(element([[matr_sample]],row_pos))
Where:
[[matr_sample]]=../matr_sample

Variable row_minima
row_minima = makearray(least(element([[matr_sample]],place_in(1))),3)
Where:
[[matr_sample]]=../matr_sample

Variable row_pos
row_pos = posleast([row_minima])

Variable value_minima
value_minima = least([row_minima])

Equations in nxn

Variable col_filter
col_filter = if rand_pos_col==1 then 2 elseif rand_pos_col==tot_col then tot_col-1 else rand_pos_col

Variable rand_pos_col
rand_pos_col = floor(rand_const(1,tot_col))

Variable rand_pos_row
rand_pos_row = floor(rand_const(1,tot_rows))

Variable row_filter
row_filter = if rand_pos_row==1 then 2 elseif rand_pos_row==tot_rows then tot_rows-1 else rand_pos_row

Variable tot_col
tot_col = count(element([[nxn_matr]],1))
Where:
[[nxn_matr]]=../nxn_matr

Variable tot_rows
tot_rows = count([[nxn_matr]])
Where:
[[nxn_matr]]=../nxn_matr

Equations in final_3x3

Variable 3x3_matr
3x3_matr = makearray(makearray(element(element([[nxn_matr]],place_in(2)+final_filter_row-2),place_in(1)+final_filter_col-2),3),3)
Where:
[[nxn_matr]]=../nxn_matr

Variable final_filter_col
final_filter_col = if which_col==tot_col then tot_col-1 else which_col
Where:
tot_col=../nxn/tot_col

Variable final_filter_row
final_filter_row = if which_row==tot_rows then tot_rows-1 else which_row
Where:
tot_rows=../nxn/tot_rows

Variable which_col
which_col = if time()==0 then col_filter else col_pos
Where:
col_pos=../3x3/col_pos
col_filter=../nxn/col_filter

Variable which_row
which_row = if time()==0 then row_filter else row_pos

Where:
row_filter=../nxn/row_filter
row_pos=../3x3/row_pos[b]

Forums: 

Circular references can be allowed by setting a new property of the influence arrow: “Use values made in same time step” to true, allowing the loop to be processed - if it makes sense otherwise.

If you email me the model file (jonathan (at) simulistics.com) I could have a look to ensure that “Use values made in same time step” is appropriate it would be easier to work with the model in Simile than look at the equation listing.

Jonathan.

Thank you Jonathan
I will send you my model right away

Thank you for your suggestion, my model compiled and worked.
Can you tell me what is the good way to stop my loop?

Hi,

Iterative loops can be stopped with the iteration (alarm) symbol. Search for iteration in the help.

The model BallBerry4a.sml in the Examples folder shows the Iteration symbol in use.