cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
midori555
Level II

Append matrices from a loop

I am trying to generate a matrix with the following scripts and have a few questions:

1. in order to correctly get "row" do I need to first sort the fruit column? Is there a Random formula to pick just one number out of "r"?

2. I need the final "run" matrix with the furitName info (the i variable) and be just one matrix. The current script I had would generate 3 run matrices (depending on the N Items(fruitName))

3. Once all run matrices are concatenated in one, is there a way to output the matrix to a data table? 

 

TIA!

 

 

Names Default To Here( 1 );
dt = Current Data Table();
Summarize( dt, fruitName = by( :fruit ) );
For( i = 1, i <= N Items( fruitName ), i++,
	r = dt << get rows where( :fruit == fruitName[i] );
	row = Random Integer( Max( r ) - Min( r ) );
/*show(r);
show(row);*/
	runTime = 5;
	run = [](0, 2);
	For( j = 1, j <= runTime, j++,
		row = Random Integer( Max( r ) - Min( r ) );
		run |/= Matrix( {{j, dt:name( "price hi" )[row] - dt:price lo[row]}} );
	);
	Show( run );
);

 

1 ACCEPTED SOLUTION

Accepted Solutions
txnelson
Super User

Re: Append matrices from a loop

Here are a couple of ways to do what I think you are looking for

Names Default To Here( 1 );
dt = Current Data Table();
Summarize( dt, fruitName = by( :fruit ) );
runTime = 5;
run = [](0, 2);
fruitList ={};
For( i = 1, i <= N Items( fruitName ), i++,
	r = dt << get rows where( :fruit == fruitName[i] );
	
	For( j = 1, j <= runTime, j++,
		insert into(fruitList, fruitName[i]);
		row = r[Random Integer( nrows(r))];

		run |/= Matrix( {{j, dt:name( "price hi" )[row] - dt:price lo[row]}} );
	);
);

dtNew = New Table("Random",
	add rows(n rows(run)),
	new column("Fruit", character,set each value(fruitList[Row()])),
	new column("Delta", set each value(run[row(),2]))
);

// or

dtNew2 = dt << Subset(
	Sample Size( runTime ),
	Selected columns only( 0 ),
	Stratify( :fruit )
);
dtNew2 << New Column( "Delta", set each value( :price hi - :price lo ) );
dtNew2 << delete columns( {"price hi", "price lo"} );
Jim

View solution in original post

2 REPLIES 2
txnelson
Super User

Re: Append matrices from a loop

Here are a couple of ways to do what I think you are looking for

Names Default To Here( 1 );
dt = Current Data Table();
Summarize( dt, fruitName = by( :fruit ) );
runTime = 5;
run = [](0, 2);
fruitList ={};
For( i = 1, i <= N Items( fruitName ), i++,
	r = dt << get rows where( :fruit == fruitName[i] );
	
	For( j = 1, j <= runTime, j++,
		insert into(fruitList, fruitName[i]);
		row = r[Random Integer( nrows(r))];

		run |/= Matrix( {{j, dt:name( "price hi" )[row] - dt:price lo[row]}} );
	);
);

dtNew = New Table("Random",
	add rows(n rows(run)),
	new column("Fruit", character,set each value(fruitList[Row()])),
	new column("Delta", set each value(run[row(),2]))
);

// or

dtNew2 = dt << Subset(
	Sample Size( runTime ),
	Selected columns only( 0 ),
	Stratify( :fruit )
);
dtNew2 << New Column( "Delta", set each value( :price hi - :price lo ) );
dtNew2 << delete columns( {"price hi", "price lo"} );
Jim

Re: Append matrices from a loop

This function might be useful.

 

matrix.PNG