 
 
 
 
 
 
ShiftLeft
- To: mathgroup at smc.vnet.net
- Subject: [mg28202] ShiftLeft
- From: "Carl K. Woll" <carlw at u.washington.edu>
- Date: Wed, 4 Apr 2001 04:13:25 -0400 (EDT)
- Organization: University of Washington
- Sender: owner-wri-mathgroup at wolfram.com
I recently wanted a function which shifts elements to the left, while
inserting zeros on the right. I also wanted it to be as quick as possible.
In case anyone else wanted this functionality, I thought it would be useful
to post to the newsgroup. The function I came up with only works with
rectangular matrices, and  it is about 3-6 times slower than RotateLeft
(depending on the size of the matrix), so I would guess that the speed is
pretty close to optimal. Here is the function:
ShiftLeft[a_,seq_List]:=PadLeft[a,Take[Dimensions[a],Length[seq]],0,seq]
Here is an example of ShiftLeft in action:
In[21]:=
ShiftLeft[{{a,b,c},{d,e,f}},{0,1}]
ShiftLeft[{{a,b,c},{d,e,f}},{0,-1}]
Out[21]=
{{b, c, 0}, {e, f, 0}}
Out[22]=
{{0, a, b}, {0, d, e}}
The second example shows that ShiftLeft can also easily shift right.
Perhaps somebody can come up with a quicker function?
Carl Woll
Physics Dept
U of Washington

