<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Optimize the loop in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514700#M74165</link>
    <description>&lt;P&gt;Thank Jim. It works like charm&lt;/P&gt;</description>
    <pubDate>Mon, 27 Jun 2022 22:57:47 GMT</pubDate>
    <dc:creator>Jackie_</dc:creator>
    <dc:date>2022-06-27T22:57:47Z</dc:date>
    <item>
      <title>Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514558#M74156</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;I have some JSL code that takes a long time to execute for larger datasets.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The code runs a for loop within a for loop to calculate the mean offset by strips. I’m looking for a faster way of doing this.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;Is there a way to make the processing faster? The current script takes ~20 mins to complete the loop&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's the script below:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );

dt1 =Data table( "Reference data table.jmp" );
dt2 = Current data table(); /// Select Data table.jmp
Col_List1 = dt2 &amp;lt;&amp;lt; Get Column Names( "String" );

For( c = 1, c &amp;lt;= N Col( dt2 ) -1, c++,
	theMatrix = [];
	name = Column( Col_List1[c] ) &amp;lt;&amp;lt; Get name;
	if (Contains( Col_List1[c], "Currents" )| Contains( Col_List1[c], "Voltage" )| Contains( Col_List1[c], "Resistance" ),
	For( r = 1, r &amp;lt;= N Rows( dt2 ), r++,
		mean = dt1:Mean[dt1 &amp;lt;&amp;lt; Get Rows Where( :Tests == name )];
		measure = mean - Col Mean( If( dt2:Strips == dt2:Strips[r], Column( dt2, c ), . ) );
		theMatrix = theMatrix || Column( dt2, c )[r] + measure;
	);
	Column( dt2, c ) &amp;lt;&amp;lt; Set Values( theMatrix );
););&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 17:02:31 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514558#M74156</guid>
      <dc:creator>Jackie_</dc:creator>
      <dc:date>2023-06-09T17:02:31Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514587#M74157</link>
      <description>&lt;P&gt;The 2 places I see issues, are the&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mean = dt1:Mean[dt1 &amp;lt;&amp;lt; Get Rows Where( :Tests == name )];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;measure = mean - Col Mean( If( dt2:Strips == dt2:Strips[r], Column( dt2, c ), . ) );&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here is a work around for the issues&amp;nbsp; Check them out and see if the answers are correct&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
start=today();
dt1 =Data table( "Reference data table.jmp" );
dt2 = Current data table(); /// Select Data table.jmp

For(i=1,i&amp;lt;=nrows(dt1),i++,
	theTest = dt1:Tests[i];
	try(column(dt2,theTest) &amp;lt;&amp;lt; set property("TheMean", dt1:mean[i]));
);

Col_List1 = dt2 &amp;lt;&amp;lt; Get Column Names( "String" );

For( c = 1, c &amp;lt;= N Col( dt2 ) -1, c++,
	theMatrix = [];
	name = Column( Col_List1[c] ) &amp;lt;&amp;lt; Get name;
	if (Contains( Col_List1[c], "Currents" )| Contains( Col_List1[c], "Voltage" )| Contains( Col_List1[c], "Resistance" ),
	//For( r = 1, r &amp;lt;= N Rows( dt2 ), r++,
	For Each Row( r=row();
		//mean = dt1:Mean[dt1 &amp;lt;&amp;lt; Get Rows Where( :Tests == name )];
		mean = column(dt2,col_list1[c]) &amp;lt;&amp;lt; get property("theMean");
		//measure = mean - Col Mean( If( dt2:Strips == dt2:Strips[r], Column( dt2, c ), . ) );
		measure = mean - Col Mean(as Column( dt2, c ),:Strips);
		theMatrix = theMatrix || Column( dt2, c )[r] + measure;
	);
	Column( dt2, c ) &amp;lt;&amp;lt; Set Values( theMatrix );
););
show((start-today())/60);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 19:57:51 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514587#M74157</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-06-27T19:57:51Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514593#M74159</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/17878"&gt;@Jackie_&lt;/a&gt;&amp;nbsp;, I'm not sure what the conditional &lt;CODE class=" language-jsl"&gt;dt2:Strips == dt2Strips[r]&lt;/CODE&gt; is supposed to resolve to, but it is probably not doing what you want it to do.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Without any test data to test on, here is how I would start optimizing this -- the main one is to take the dt1 &lt;CODE class=" language-jsl"&gt;&amp;lt;&amp;lt; Get Rows Where( :Tests == name )&lt;/CODE&gt; outside of the loop.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here( 1 );

dt1 = Data Table( "Reference Data Table.jmp" );
dt2 = Current Data Table();

col list1 = dt2 &amp;lt;&amp;lt; Get Column Names( "String" );

Summation( c = 1, N Col( dt2 ) - 1,  //summation is faster that For() if you don't need to use Break() or Continue()
	values = [];
	name = Column( dt2, c ) &amp;lt;&amp;lt; Get Name;
	If( Contains( name, "Currents" ) | Contains( name, "Voltage" ) | Contains( name, "Resistance" ),
		rows = dt1 &amp;lt;&amp;lt; Get Rows Where( :tests == name );		// As Constant() prevents JMP from doing a lookup per row
		col = Column( dt2, c );
		col mean = Col Mean( col );
		For Each Row( dt2,
			mean = dt1:Mean[rows];
			measure = mean - If( dt2:Strips == dt2:Strips /* always true */, col mean, 0 );
			values |/= col[] + measure 	// append value to
		);
		col &amp;lt;&amp;lt; Set Values( values )
	);
	0	// summation needs to end in a numeric
)&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 27 Jun 2022 22:14:38 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514593#M74159</guid>
      <dc:creator>ErraticAttack</dc:creator>
      <dc:date>2022-06-27T22:14:38Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514700#M74165</link>
      <description>&lt;P&gt;Thank Jim. It works like charm&lt;/P&gt;</description>
      <pubDate>Mon, 27 Jun 2022 22:57:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/514700#M74165</guid>
      <dc:creator>Jackie_</dc:creator>
      <dc:date>2022-06-27T22:57:47Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/526818#M75075</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2687"&gt;@txnelson&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;There's some issue in the logic that you suggested. If the reference data table doesn't contain the test that are in the main data table, it will delete the row values from those tests. I only to offset the test that are in the reference data table. Any help would be much appreciated&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jacksmith12_2-1658873805995.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/44351i1710764982D25466/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jacksmith12_2-1658873805995.png" alt="Jacksmith12_2-1658873805995.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I have attached the data tables below. I only want to offset the tests that are specified in the reference table&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jul 2022 22:44:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/526818#M75075</guid>
      <dc:creator>Jackie_</dc:creator>
      <dc:date>2022-07-26T22:44:48Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/526835#M75078</link>
      <description>&lt;P&gt;I modified your code to bypass the processing if the Column Property "theMean" had not been set&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
start = Today();
dt1 = Data Table( "Reference data table.jmp" );
dt2 = Current Data Table(); /// Select Data table.jmp

For( i = 1, i &amp;lt;= N Rows( dt1 ), i++,
	theTest = dt1:Tests[i];
	Try( Column( dt2, theTest ) &amp;lt;&amp;lt; set property( "TheMean", dt1:mean[i] ) );
);

Col_List1 = dt2 &amp;lt;&amp;lt; Get Column Names( "String" );

For( c = 1, c &amp;lt;= N Col( dt2 ) - 1, c++,
	theMatrix = [];
	name = Column( Col_List1[c] ) &amp;lt;&amp;lt; Get name;
	If( Contains( Col_List1[c], "Currents" ) | Contains( Col_List1[c], "Voltage" ) | Contains( Col_List1[c], "Resistance" ), 
	//For( r = 1, r &amp;lt;= N Rows( dt2 ), r++,
		mean = .;
		Try( mean = Column( dt2, col_list1[c] ) &amp;lt;&amp;lt; get property( "theMean" ) );
		// Don't process if a value for theMean does not exist
		If( Is Missing( mean ) == 0,
			For Each Row(
				r = Row();
		//mean = dt1:Mean[dt1 &amp;lt;&amp;lt; Get Rows Where( :Tests == name )];
		
				//measure = mean - Col Mean( If( dt2:Strips == dt2:Strips[r], Column( dt2, c ), . ) );
				measure = mean - Col Mean( As Column( dt2, c ), :Strips );
				theMatrix = theMatrix || Column( dt2, c )[r] + measure;
			);
			Column( dt2, c ) &amp;lt;&amp;lt; Set Values( theMatrix );
		);
	);
);
Show( (start - Today()) / 60 );&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jul 2022 00:32:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/526835#M75078</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-07-27T00:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Optimize the loop</title>
      <link>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/527004#M75087</link>
      <description>&lt;P&gt;Thanks a lot Jim.&lt;/P&gt;</description>
      <pubDate>Wed, 27 Jul 2022 13:16:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Optimize-the-loop/m-p/527004#M75087</guid>
      <dc:creator>Jackie_</dc:creator>
      <dc:date>2022-07-27T13:16:14Z</dc:date>
    </item>
  </channel>
</rss>

