cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Don’t miss your chance to experience Discovery Summit Europe at our best available rate! Early bird registration through 31 Oct.
  • Graph Builder Toolbar streamlines interactive graphing. Creates shortcuts on Report and GraphBuilder Helpers toolbars. Download and install the extension.
  • JMP will suspend normal business operations for our Rest and Recharge Day on Friday, October 2, 2026.
    Regular business hours will resume on Monday, October 5, 2026.

Discussions

Solve problems, and share tips and tricks with other JMP users.
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

Recommended Articles