<?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: how to shift rows of matrix by amount in SAS/IML Software and Matrix Computations</title>
    <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979375#M6554</link>
    <description>&lt;P&gt;Alternative with the insert() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

B=j(1,c,.);
  do i = 1 to r*(max(v) &amp;lt;= c);
    x = a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
    B = insert(B, x, i, 0);
  end;
B = B[1:r,];
print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT:]&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or with the shape() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

do i = 1 to r*(max(v) &amp;lt;= c);
  B = B || a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
end;

B = shape(B,r,c);
print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT 2:]&lt;/P&gt;
&lt;P&gt;Or with just proper sub-scripting:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

B=j(r,c,.);

do i = 1 to r*(max(v) &amp;lt;= c);
  B[i,1:c] = a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
end;

print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
    <pubDate>Sat, 22 Nov 2025 18:03:22 GMT</pubDate>
    <dc:creator>yabwon</dc:creator>
    <dc:date>2025-11-22T18:03:22Z</dc:date>
    <item>
      <title>how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979370#M6552</link>
      <description>&lt;DIV class="yiv1350428736elementToProof"&gt;I want shift numbres in each row of matrux.&amp;nbsp; if row is {1 2 3 4} and shift amount is 1, I want row to be {4 1 2 3}. Numbers wrap back when they poke off end. Here bigger example. (Real matrix have &amp;gt; 1000 rows!)&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;PROC IML;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;A = {1 2 3 4,&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;5 6 7 8,&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;9 0 1 2} ;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;v = {1,3,2} ;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;/* want shift rows of A by the numbers in v. If pokes out endf wrap on other side */&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;Want = {4 1 2 3 &amp;nbsp; &amp;nbsp; /* shift by 1 */&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 6 7 8 5 &amp;nbsp; &amp;nbsp; /* shift by 3 */&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 1 2 9 0} ; &amp;nbsp;/* shift by 2 */&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;QUIT;&lt;/DIV&gt;
&lt;DIV class="yiv1350428736elementToProof"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Sat, 22 Nov 2025 12:25:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979370#M6552</guid>
      <dc:creator>WeiChen</dc:creator>
      <dc:date>2025-11-22T12:25:45Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979374#M6553</link>
      <description>&lt;P&gt;How about something like this for start:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);
  do i = 1 to r*(max(v) &amp;lt;= c);
    
    x = a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
    B = B // x;
  end;

print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;?&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 22 Nov 2025 17:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979374#M6553</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-22T17:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979375#M6554</link>
      <description>&lt;P&gt;Alternative with the insert() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

B=j(1,c,.);
  do i = 1 to r*(max(v) &amp;lt;= c);
    x = a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
    B = insert(B, x, i, 0);
  end;
B = B[1:r,];
print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT:]&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or with the shape() function:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

do i = 1 to r*(max(v) &amp;lt;= c);
  B = B || a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
end;

B = shape(B,r,c);
print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;[EDIT 2:]&lt;/P&gt;
&lt;P&gt;Or with just proper sub-scripting:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

B=j(r,c,.);

do i = 1 to r*(max(v) &amp;lt;= c);
  B[i,1:c] = a[i,(c-v[i])+1:c] || a[i,1:(c-v[i])];
end;

print B;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sat, 22 Nov 2025 18:03:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979375#M6554</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-22T18:03:22Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979377#M6555</link>
      <description>&lt;PRE&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);
idx=c-v+1;
AA=A||A;  
want=j(r,c,.);
do i=1 to r;
 want[i,]=AA[i,idx[i]:idx[i]+c-1];
end;
print want;
quit;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Nov 2025 07:31:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979377#M6555</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2025-11-23T07:31:29Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979379#M6556</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;thanks for reminding about the &lt;STRONG&gt;AA=A||A&lt;/STRONG&gt; trick!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now it can be done "loop-less" with "one" expression (see below):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

/* step by step */
AA=A||A;  
x=1:2*c;
y1 = repeat(x, r, 1);
y2 = y1 + (c-v);
y3 = y2[,1:c];
y4 = y3 + t(0:r-1)*c*2;
print x,y1,y2,y3,AA,y4;
want = shape(AA[y4],r);
print want;

/* one expression - in Klingons language */
want2 = shape((A||A)[(repeat(1:2*c,r,1)+(c-v))[,1:c]+t(0:r-1)*c*2],r);
print want2;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1763892284478.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/111503i569BFCC247C92796/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1763892284478.png" alt="yabwon_0-1763892284478.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;All the best&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 10:06:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979379#M6556</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-23T10:06:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979381#M6557</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/18408"&gt;@Ksharp&lt;/a&gt;&amp;nbsp;, even shorter:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

/* step by step */
AA=A||A; /* trick by @Ksharp */  
x=1:2*r*c;
y1=shape(x,r);
y2=(y1+(c-v))[,1:c];
print x,y1,y2;
want3 = shape(AA[y2],r);
print want3;

/* one expression - in less Klingons language */
want4 = shape((A||A)[(shape(1:2*r*c,r)+(c-v))[,1:c]],r);
print want4;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1763893078566.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/111504i681D442366098522/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1763893078566.png" alt="yabwon_0-1763893078566.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 10:18:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979381#M6557</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-23T10:18:14Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979383#M6558</link>
      <description>&lt;P&gt;Can't help myself &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt; &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;PROC IML;
A = {1 2 3 4,
     5 6 7 8,
     9 0 1 2} ;
v = {1,3,2} ;

r=nrow(A);
c=ncol(A);

/* step by step */
AA=A||A; /* trick by @Ksharp */  
v1 = SHAPE(1:2*r*c,r);
v2 = REPEAT(c-v, 1, 2*c); 
v3 = (v2+v1)[,1:c];
want5 = shape(AA[v3],r);
print AA,v,v1,v2,v3,want5;

/* one expression - in yet another Klingons language */
want6 = shape((A||A)[(REPEAT(c-v,1,2*c)+SHAPE(1:2*r*c,r))[,1:c]],r);
print want6;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1763894010788.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/111505iE9D17EFF6E8826DD/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1763894010788.png" alt="yabwon_0-1763894010788.png" /&gt;&lt;/span&gt;&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;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 10:33:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979383#M6558</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-23T10:33:47Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979384#M6559</link>
      <description>&lt;P&gt;It sounds like the OP is requesting a CYCLIC shift of each row. A cyclic shift can be accomplished by using the MOD function as follows. Let p=NCOL(A).&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Create a vector that has the indices 1:p&lt;/LI&gt;
&lt;LI&gt;Shift the indices to the right: j -&amp;gt;&amp;nbsp; p - v[i] + j&lt;/LI&gt;
&lt;LI&gt;The MOD function works best for 0-based indices, whereas IML uses 1-based indices. So subtract 1 to get a 0-based index, apply the (MOD p) operation, then add 1 back to return to 1-based indices.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;For example, the OP's example, has p=4, so you can use the following to create the shift for the i_th row:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;   idx = 1:p;
   shiftIdx = p - v[i] + idx;          * shift to the right ;
   newIdx = mod(shiftIdx-1, p) + 1;    * wrap around ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Here's how it looks being applied to each row:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* cyclic rotation of row elements */
proc iml;
A = {1 2 3 4, 
     5 6 7 8, 
     9 0 1 2} ;
v = {1,3,2} ;
p = ncol(A);
idx = 1:p;

/* Demonstrate using the MOD function to shift the indices */
/*
do i = 1 to nrow(v);
   shiftIdx = p - v[i] + idx;          * shift to the right ;
   newIdx = mod(shiftIdx-1, p) + 1;    * wrap around ;
   print (v[i])[L='shift'], newIdx[c=('c1':'c4')];
end;
*/

/* Solve the real problem: use MOD function to apply a cyclic shift of row A[i,] 
   by the number of columns in v[i] */
ShiftA = j(nrow(A), p, .);  /* allocate */
do i = 1 to nrow(A);
   shiftIdx = p - v[i] + idx;      /* shift to the right */
   newIdx = mod(shiftIdx-1, p) + 1; /* wrap around */
   *print i adjIdx;
   ShiftA[i,] = A[i, newIdx];
end;

print ShiftA;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 11:04:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979384#M6559</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-11-23T11:04:08Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979385#M6560</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13684"&gt;@Rick_SAS&lt;/a&gt;&amp;nbsp;, vectorised version:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc iml;
A = {1 2 3 4, 
     5 6 7 8, 
     9 0 1 2} ;
v = {1,3,2} ;

c = ncol(A);
r = nrow(A);
idx = repeat(1:c,r);
row = t(0:r-1)*c;
print idx, row;

/* vectorised */
shiftIdx = c - v + idx;
print shiftIdx;
newIdx = mod(shiftIdx-1, c) + 1; 

want = shape(A[newIdx + row], r);
print want;

/* one expression */
want2 = shape(A[(mod((c - v + idx)-1, c) + 1) + t(0:r-1)*c], r);
print want2;

quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Output:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="yabwon_0-1763897544185.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/111506iB2BAFFF309539F73/image-size/medium?v=v2&amp;amp;px=400" role="button" title="yabwon_0-1763897544185.png" alt="yabwon_0-1763897544185.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 11:32:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979385#M6560</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-23T11:32:39Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979389#M6561</link>
      <description>&lt;P&gt;Yes, and you can use &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_069/imlug/imlug_langref_sect082.htm" target="_self"&gt;the COL function&lt;/A&gt; to simplify the creation of the matrix for which M[i,j]=j.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;idx = col(A);
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The rest of your program uses a formula to &lt;A href="https://blogs.sas.com/content/iml/2011/02/16/converting-matrix-subscripts-to-indices.html" target="_self"&gt;convert between subscripts and indices&lt;/A&gt;. The &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_069/imlug/imlug_langref_sect283.htm" target="_self"&gt;NDX2SUB&lt;/A&gt;&amp;nbsp;and &lt;A href="https://go.documentation.sas.com/doc/en/pgmsascdc/v_069/imlug/imlug_langref_sect489.htm" target="_self"&gt;SUB2NDX&lt;/A&gt; functions incorporate this functionality and are sometimes useful in situations like this.&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 11:49:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979389#M6561</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-11-23T11:49:54Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979390#M6562</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;BTW, a cyclic shift is one kind of a permutation. If the OP were interested in more general permutations, they could use the ideas in the article&amp;nbsp;&lt;A href="https://blogs.sas.com/content/iml/2014/05/29/permute-elements-within-each-row-of-a-matrix.html" target="_blank"&gt;Permute elements within each row of a matrix - The DO Loop&lt;/A&gt;, which shows how to apply a random permutation to each row of a matrix in a vectorized manner.&amp;nbsp; Using the program in that article, I suspect you can write a new function called CyclicPerm and replace the call to RANPERM.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 12:09:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979390#M6562</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-11-23T12:09:17Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979396#M6563</link>
      <description>&lt;P&gt;Thank you every body who respond. I like MOD function. very elgelant. Also like vectorize program. Need to make timeing to see if makes differnce. My matrix have 15 column and 10.000 row.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/35763"&gt;@yabwon&lt;/a&gt;&amp;nbsp;Are you learning IML? I see you answer other forums like macro and dataq step, but not often IML.&lt;/P&gt;</description>
      <pubDate>Sun, 23 Nov 2025 21:45:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979396#M6563</guid>
      <dc:creator>WeiChen</dc:creator>
      <dc:date>2025-11-23T21:45:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979410#M6564</link>
      <description>&lt;P&gt;If there's a question I try to help. I don't distinguish "forums". They all the same SAS programming questions to me.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Bart&lt;/P&gt;</description>
      <pubDate>Mon, 24 Nov 2025 08:10:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979410#M6564</guid>
      <dc:creator>yabwon</dc:creator>
      <dc:date>2025-11-24T08:10:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to shift rows of matrix by amount</title>
      <link>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979424#M6565</link>
      <description>&lt;P&gt;For 10,000 rows,&amp;nbsp; both the loop and the vectorized methods have approximately the same run time. On my laptop, the looping method takes 0.007 s and the vectorized method takes 0.004 s.&amp;nbsp; On the one hand, the vectorized method is almost twice as fast. On the other hand, you save only 0.003 s by using it.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If your matrix has 100,000 or more rows, then the performance comparison is:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;100,000 rows: the looping method takes 0.07 s and the vectorized method takes 0.04 s.&amp;nbsp;&lt;/LI&gt;
&lt;LI&gt;1,000,000 rows: the looping method takes 0.7 s and the vectorized method takes 0.4 s.&amp;nbsp;&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here is the program I use, so you can run your own experiments:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/* Rick's original version: Loop over rows and apply each shift */
%let NCOL = 15;
%let NROW = 1000000;

proc iml;
nr = &amp;amp;NROW;
p = &amp;amp;NCOL;
A = shape(1:(nr*p), nr, p);

call randseed(1234);
v = randfun(nr, "Uniform", 0, p-1);  /* create shift vector */
nRep = 10;    /* repeat the experiment nRep times and report average */

/* use MOD function to apply a cyclic shift of row A[i,] 
   by the number of columns in v[i] */
t0 = time();
do rep = 1 to nRep;
   idx = 1:p;
   ShiftA = j(nr, p, .);  /* allocate */
   do i = 1 to nr;
      shiftIdx = p - v[i] + idx;      /* shift to the right */
      newIdx = mod(shiftIdx-1, p) + 1; /* wrap around */
      ShiftA[i,] = A[i, newIdx];
   end;
end;
tLoop = (time() - t0) / nRep;

/* yabwon's vectorized version */
t0 = time();
do rep = 1 to nRep;
   c = ncol(A);
   r = nrow(A);
   idx = repeat(1:c,r);
   row = t(0:r-1)*c;
   shiftIdx = c - v + idx;
   newIdx = mod(shiftIdx-1, c) + 1; 
   want = shape(A[newIdx + row], r);
end;
tVectorized = (time() - t0) / nRep;

print tLoop tVectorized;
print (max(abs(ShiftA - want)))[L="Diff between answers"];&lt;/CODE&gt;&lt;/PRE&gt;
&lt;DIV class="branch"&gt;&amp;nbsp;&lt;/DIV&gt;</description>
      <pubDate>Mon, 24 Nov 2025 11:25:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-IML-Software-and-Matrix/how-to-shift-rows-of-matrix-by-amount/m-p/979424#M6565</guid>
      <dc:creator>Rick_SAS</dc:creator>
      <dc:date>2025-11-24T11:25:17Z</dc:date>
    </item>
  </channel>
</rss>

