Choose Language Hide Translation Bar
Highlighted
vistacc
Level III

use while loop to add rows

Hi all,

I want to continously add rows to an empty data table until a condition is met but I am not sure what to do.

In the following example, the condition is whether all elements in the vector in "steady state" are ones.

ret = New Table( title,
	New Column( "P", Numeric, "Continuous", Formula( ptable << get as matrix ) ),
	New Column( "X(n)",
		Expression,
		"None",
		Formula( If( Row() == 1, init_state, Lag( :Name( "X(n)" ), 1 ) * :P ) ),
		Set Selected,

	),
	New Column( "steady state",
		Expression,
		"None",
		Formula( :Name( "X(n)" ) - Lag( :Name( "X(n)" ) ) == zeros ),

	),
	New Column( "States", Character, Values( names ) ),

);

P and init_state are matrices read from a data table. Row values in X(n) are the multiply of the last row values and P. Values in "steady state" column are vectors that consist of zeros and ones, representing whether the elements in two adjacent X(n) vectors are equal or not. I want to add rows to this data table until the vector in "steady state" consists of all ones (i.e. the two adjacent vectors in X(n) are completely equal).

rownum = N Rows( ret );
cols = Column( ret, "steady state" );
ones = J( 1, vectorlength, 1 );
While((!vectorEqual(cols[rownum],ones)),
	// add rows here, not sure how
	rownum = N Rows( ret );
)

Another question is that I add command "show(cols[rownum])" to check whether the selected value is correct but it shows Empty()... Is it because that since all values are not added to the table at the first place so I cannot refer to them? If so, how can I check for the condition?

 

I would greatly appreciate any help!

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
txnelson
Super User

Re: use while loop to add rows

Concerning the adding of rows, I assume that all you need to do is to add

rownum = N Rows( ret );
cols = Column( ret, "steady state" );
ones = J( 1, vectorlength, 1 );
While((!vectorEqual(cols[rownum],ones)),
	ret << add rows(1);
        dt << Rerun Formulas;
	rownum = N Rows( ret );
)
Jim

View solution in original post

3 REPLIES 3
Highlighted
txnelson
Super User

Re: use while loop to add rows

Concerning the adding of rows, I assume that all you need to do is to add

rownum = N Rows( ret );
cols = Column( ret, "steady state" );
ones = J( 1, vectorlength, 1 );
While((!vectorEqual(cols[rownum],ones)),
	ret << add rows(1);
        dt << Rerun Formulas;
	rownum = N Rows( ret );
)
Jim

View solution in original post

Highlighted
vistacc
Level III

Re: use while loop to add rows

Thank you so much! it works!

I still have trouble getting the cell value in the last row of the columns. As mentioned, it shows "Empty()" in the Log window. I guess the problem is that those values are assigned using formulas so they are Empty() when I tried to access them. What scripts should I use to get the values of those cells?

Highlighted
vistacc
Level III

Re: use while loop to add rows

Never mind. I found adding "Rerun Formulas;" can solve the problem. Thank you!

Article Labels

    There are no labels assigned to this post.