Re: Could this be improved?
- To: mathgroup at smc.vnet.net
- Subject: [mg21763] Re: [mg21741] Could this be improved?
- From: "David Park" <djmp at earthlink.net>
- Date: Thu, 27 Jan 2000 22:56:43 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
Jordan, This might be somewhat simpler. We reverse the vector, pad it on the ends, and then Take a sliding section: myMtx[v_?VectorQ] := Module[{w, l = Length[v]}, w = Join[Table[0, {l - 1}], Reverse[v], Table[0, {l - 1}]]; Table[Take[w, {2l - i, 3l - 1 - i}], {i, 1, 2l - 1}] ] myMtx[{1, 2, 3}] {{1, 0, 0}, {2, 1, 0}, {3, 2, 1}, {0, 3, 2}, {0, 0, 3}} myMtx[{1, 2, 3, 4}] {{1, 0, 0, 0}, {2, 1, 0, 0}, {3, 2, 1, 0}, {4, 3, 2, 1}, {0, 4, 3, 2}, {0, 0, 4, 3}, {0, 0, 0, 4}} David Park djmp at earthlink.net http://home.earthlink.net/~djmp/ >Hi all, > >I wrote the following code which works correctly. I was wondering, however, >if there was a way of doing the same thing that had more of a Mathematica >approach. I am new to Mathematica and am still trying to get a grasp on how >to program effectively within the environment. > >myMtx[v_] := Module[ > {nCols, nRows, vPadded, c}, > nCols = Length[v]; > nRows = 2nCols - 1; > c = ZeroMatrix[nRows, nCols]; > vPadded = PadRight[v, nRows, 0]; > For[i = 1, i <= nCols, i++, > c[[All, i]] = vPadded; > vPadded = RotateRight[vPadded] > ]; > c > ] > >For example, myMtx[{1,2,3}] takes the vector {1,2,3} and turns it into the >matrix {{1, 0, 0}, {2, 1, 0}, {3, 2, 1}, {0, 3, 2}, {0, 0, 3}} which looks >like > >[ 1 0 0 ] >[ 2 1 0 ] >[ 3 2 1 ] >[ 0 3 2 ] >[ 0 0 3 ] > > >Thanks, > >Jordan >