<?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: Alternative to Lag() when working with matrices instead of data tables in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57954#M32226</link>
    <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2610"&gt;@vince_faller&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Can you help me implement the following in a matrix without a loop ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt  = New Table("Test"); 

dt &amp;lt;&amp;lt; New Column("RandomList",Numeric,Continuous,&amp;lt;&amp;lt; Set Values(Random Index(10^3,10^2)))
   &amp;lt;&amp;lt; New Column("DesFunc",Numeric,Continuous,Formula(If(Row() == 1,0,If(Mod(Lag(:RandomList),2)==0,:RandomList[Row()]+1,:RandomList[Row()]-1))));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Also, I couldn't find the apply() or applyOver() functions in my scripting index. I am using JMP 14 . Is it a JMP Pro thing ?&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="image.png" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/10807iEA88FFF91B092574/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 23 May 2018 14:24:28 GMT</pubDate>
    <dc:creator>uday_guntupalli</dc:creator>
    <dc:date>2018-05-23T14:24:28Z</dc:date>
    <item>
      <title>Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57900#M32198</link>
      <description>&lt;P&gt;All,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Is there an alternative to the lag() that I could use when working with matrices instead of data tables ? Esentially, I would like to be able to look at the value of the previous row of my column in a matrix to determine the value of current row. This can easily be acheived using the lag() function in a data table. What is an effective way other than looping to acheive this if working with matrices ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table("Test"); 

dt &amp;lt;&amp;lt; New Column("Random-1",Numeric,Continuous,&amp;lt;&amp;lt; set values(Random Index(10^3,10^2))); 

dt &amp;lt;&amp;lt; New Column("AltToLag",Numeric,Continuous,Formula(If(Row()==1,0,Lag(:Name("Random-1")))));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 22 May 2018 20:48:04 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57900#M32198</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-22T20:48:04Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57902#M32200</link>
      <description>&lt;P&gt;Does this work for you?&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;names default to here(1);
lag_matrix = function({mat, lag}, {DEFAULT LOCAL},
	n = nrows(mat);
	lag_vector = 1::n;
	empty_mat = J(abs(lag), ncols(mat), .);
	if(lag&amp;gt;0, 
		lag_vector = lag_vector[1::n-abs(lag)];
		new_mat = empty_mat|/mat[lag_vector, 0];
	, //else
		lag_vector = lag_vector[abs(lag)+1::n];
		new_mat = mat[lag_vector, 0]|/empty_mat;
	);
	new_mat;
);
lag_matrix((14::28)`||(28::42)`, -1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 21:17:52 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57902#M32200</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2018-05-22T21:17:52Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57910#M32202</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2610"&gt;@vince_faller&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Thank you for the solution you have offered, is there a way to modify this function to get just the previous row rather than the entire matrix ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Maybe the appropriate question would be what is the vectorized format of looping around a matrix with the function - similar to apply functions in R which are much faster than loops.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 22 May 2018 22:22:24 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57910#M32202</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-22T22:22:24Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57935#M32214</link>
      <description>&lt;P&gt;Uday,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know what you are asking. You can just reference the row&amp;nbsp; j-1. The script shows both lag1 and dif1 results.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);

dt = Open("$sample_data/Big Class.jmp");

bcmat = dt &amp;lt;&amp;lt; get as matrix; //Matrix(40,3)

for(j=2, j&amp;lt;=nrow(dt), j++,
 lag1 = bcmat[j-1,0];
 dif1 = bcmat[j,0] - bcmat[j-1,0];
 show( lag1, dif1)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 May 2018 09:41:48 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57935#M32214</guid>
      <dc:creator>gzmorgan0</dc:creator>
      <dc:date>2018-05-23T09:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57946#M32220</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/70"&gt;@gzmorgan0&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; What I am trying to ask for is a way to avoid the loop and rely on vectorized code. It is advisable to replace loops with matrix functions to make the code more efficient (&lt;A href="https://community.jmp.com/t5/JMP-Blog/JSL-Tip-Replace-Loops-with-Functions-on-Matrices/ba-p/29783" target="_blank"&gt;https://community.jmp.com/t5/JMP-Blog/JSL-Tip-Replace-Loops-with-Functions-on-Matrices/ba-p/29783&lt;/A&gt;) . This is acheived in R through the apply family functions and in Matlab through some matrix functions or custom functions. I am requesting if there is a way to implement such a vectorized code on matrices rather than try and write a loop in this case to find the lag().&amp;nbsp;&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/70"&gt;@gzmorgan0&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;Uday,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know what you are asking. You can just reference the row&amp;nbsp; j-1. The script shows both lag1 and dif1 results.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default to Here(1);

dt = Open("$sample_data/Big Class.jmp");

bcmat = dt &amp;lt;&amp;lt; get as matrix; //Matrix(40,3)

for(j=2, j&amp;lt;=nrow(dt), j++,
 lag1 = bcmat[j-1,0];
 dif1 = bcmat[j,0] - bcmat[j-1,0];
 show( lag1, dif1)
);&lt;/CODE&gt;&lt;/PRE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;In the example you have provided, I definitely understand that it is easy to write a loop and get the lag() equivalent, however as the size of your data sets or the # of your iterations increase this will slow your code down. I am looking for an efficient replacement i.e. vectorized equivalent of implementing repetitive operations on matrices in JSL.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 12:43:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57946#M32220</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-23T12:43:06Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57947#M32221</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/494"&gt;@XanGregg&lt;/a&gt;&amp;nbsp;,&amp;nbsp;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/982"&gt;@Craige_Hales&lt;/a&gt;&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Xan or Craige- if it is possible, I would actually request you to kindly make a blog post that covers in detail how to perform vectorization with numerous examples in JSL. This topic is covered at length in SAS , R , Matlab and other environments. I would love to see something similar for JMP. If something similar already exists, would request you to draw my attention to it.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;SAS Blogs -&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2013/05/15/vectorize-computations.html&amp;nbsp;" target="_blank"&gt;https://blogs.sas.com/content/iml/2013/05/15/vectorize-computations.html&amp;nbsp;&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 12:55:14 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57947#M32221</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-23T12:55:14Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57950#M32224</link>
      <description>&lt;P&gt;What are you actually trying to do?&amp;nbsp;You're right that matrices are usually faster but looping through them will (in my experience) usually lose any benefit.&amp;nbsp; &amp;nbsp;JMP 14 now has apply() and applyOver() functions but I haven't had a lot of luck using them.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 13:35:53 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57950#M32224</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2018-05-23T13:35:53Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57954#M32226</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2610"&gt;@vince_faller&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; Can you help me implement the following in a matrix without a loop ?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt  = New Table("Test"); 

dt &amp;lt;&amp;lt; New Column("RandomList",Numeric,Continuous,&amp;lt;&amp;lt; Set Values(Random Index(10^3,10^2)))
   &amp;lt;&amp;lt; New Column("DesFunc",Numeric,Continuous,Formula(If(Row() == 1,0,If(Mod(Lag(:RandomList),2)==0,:RandomList[Row()]+1,:RandomList[Row()]-1))));&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Also, I couldn't find the apply() or applyOver() functions in my scripting index. I am using JMP 14 . Is it a JMP Pro thing ?&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="image.png" style="width: 999px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/10807iEA88FFF91B092574/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 14:24:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57954#M32226</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-23T14:24:28Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57961#M32232</link>
      <description>&lt;P&gt;It's not exactly super user friendly, but even all of these steps are still ~3 times faster (436:135 us) on my computer when running the vector vs the column.&amp;nbsp; There are definitely optimizations you could do to this to make it faster too. When I put it to 1000 rows it was ~4 times faster(1057:227us). Also, apparently they got rid of Apply, my bad.&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt = New Table( "Test" ); 

dt &amp;lt;&amp;lt; New Column( "RandomList", Numeric, Continuous, &amp;lt;&amp;lt;Set Values( Random Index( 10 ^ 3, 10 ^ 3 ) ) );
time_column = HPTime();
dt &amp;lt;&amp;lt; New Column( "DesFunc",
	Numeric,
	Continuous,
	Formula(
		If( Row() == 1,
			0,
			If( Mod( Lag( :RandomList ), 2 ) == 0,
				:RandomList[Row()] + 1,
				:RandomList[Row()] - 1
			)
		)
	)
);
time_column = HPTime()-time_column;


lag_matrix = Function( {mat, lag},
	{DEFAULT LOCAL},
	n = N Rows( mat );
	lag_vector = 1 :: n;
	empty_mat = J( Abs( lag ), N Cols( mat ), . );
	If( lag &amp;gt; 0,
		lag_vector = lag_vector[1 :: n - Abs( lag )];
		new_mat = empty_mat |/ mat[lag_vector, 0];
	, //else
		lag_vector = lag_vector[Abs( lag ) + 1 :: n];
		new_mat = mat[lag_vector, 0] |/ empty_mat;
	);
	new_mat;
);


v = Column( dt, "RandomList" ) &amp;lt;&amp;lt; Get Values;
check_vector = Column(dt, "DesFunc") &amp;lt;&amp;lt; Get Values;

time_vector = HPTime();
//lag the vector
lv = lag_matrix(v, 1);
//mod the vector
mod_vector = lv / 2 - floor(lv/2);
//find the mods you want
loc_vector = loc(mod_vector==0);
//invert to find the mods you don't want
invert_vector = J(nrows(v), 1);
invert_vector[loc_vector] = 0;
invert_vector = loc(invert_vector);
//assign values
v[loc_vector] = v[loc_vector] + 1;
v[invert_vector] = v[invert_vector] - 1;
//set first row to 0
v[1] = 0;
time_vector = HPTime()-time_vector;

show(all(v == check_vector));
show(time_column, time_vector);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Hope this helps a little.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 15:27:28 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57961#M32232</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2018-05-23T15:27:28Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57967#M32236</link>
      <description>&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/2610"&gt;@vince_faller&lt;/a&gt;,&amp;nbsp;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; Thanks for the solution, can you kindly explain the thought process and how it is functioning if it is not too much to ask.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;I see that you are using the combination of lag_matrix function,&amp;nbsp; mod function and loc() but I don't quite get the logic.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; Would greatly appreaciate a breadown of the logic so I can learn the though process ( again only if it is not too much to ask)&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 16:16:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57967#M32236</guid>
      <dc:creator>uday_guntupalli</dc:creator>
      <dc:date>2018-05-23T16:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57974#M32237</link>
      <description>&lt;P&gt;Sure.&amp;nbsp; So if we look at your formula (other than the first row = 0)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;
	If( Mod( Lag( :RandomList ), 2 ) == 0,
		:RandomList[Row()] + 1,
		:RandomList[Row()] - 1
	)

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I see it as 3 parts, the lag, the mod == 0 check of the lag, and the else expression of that.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So I first do the lag&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;lv = lag_matrix(v, 1);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then I get the mod vector&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mod_vector = lv / 2 - floor(lv/2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;but I just realized mod works on vectors so you could do&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;mod_vector = mod(lv, 2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;which is significantly faster&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then I get a boolean vector by doing&amp;nbsp;&lt;/P&gt;
&lt;P&gt;mod_vector == 0 and loc() it to find the rows where the mod(2) of the lag(1) is 0&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;loc_vector = loc(mod_vector==0);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;loc_vector is the rows that match your if statement&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Mod( Lag( :RandomList ), 2 ) == 0&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;then to get the else statement rows, I invert the selection by making a vector of all 1s and setting the loc_vector rows to 0 and doing a loc()&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;invert_vector = J(nrows(v), 1);
invert_vector[loc_vector] = 0;
invert_vector = loc(invert_vector);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;so if you concatted the two vectors and sorted, it should just be 1::nrows()&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;after that I just set the rows for the if expression to +1 and the else expression to -1&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;v[loc_vector] = v[loc_vector] + 1;
v[invert_vector] = v[invert_vector] - 1;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and finally set the first row to 0&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;v[1] = 0;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 23 May 2018 16:32:47 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57974#M32237</guid>
      <dc:creator>vince_faller</dc:creator>
      <dc:date>2018-05-23T16:32:47Z</dc:date>
    </item>
    <item>
      <title>Re: Alternative to Lag() when working with matrices instead of data tables</title>
      <link>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57988#M32238</link>
      <description>&lt;P&gt;Here's a variation of Vince's solution I was playing with. It &lt;SPAN&gt;might&amp;nbsp;&lt;/SPAN&gt;or &lt;SPAN&gt;might&amp;nbsp;&lt;/SPAN&gt;not capture the requirements but might explain how to do the manipulations you need.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;R = [4, 6, 6, 5, 8, 7];
lag = 1;
// describe the adjustment
modamount = [1, -1];
// lagged values dont need the last value
lagged = R[1 :: N Rows( R ) - lag];// [4, 6, 6, 5, 8]
// adjustment is based on lagged values. add 1 for indexing.
adjust = modamount[1 + Mod( lagged, 2 )];// [1, 1, 1, -1, 1]
// source data does not need the first value
source = R[1 + lag :: N Rows( R )]; // [6, 6, 5, 8, 7]
// combine the adjustment with the source
combine = adjust + source; //    [7, 7, 6, 7, 8]
// prepend a zero
answer = 0 |/ combine; // [0, 7, 7, 6, 7, 8]
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 23 May 2018 16:41:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Alternative-to-Lag-when-working-with-matrices-instead-of-data/m-p/57988#M32238</guid>
      <dc:creator>Craige_Hales</dc:creator>
      <dc:date>2018-05-23T16:41:25Z</dc:date>
    </item>
  </channel>
</rss>

