Re: Could this be improved?
- To: mathgroup at smc.vnet.net
- Subject: [mg21772] Re: Could this be improved?
- From: axc at gamow.ces.cwru.edu
- Date: Thu, 27 Jan 2000 22:56:53 -0500 (EST)
- Organization: Case Western Reserve University
- References: <86mdpv$2f3@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
slightly more compact:
In[1]:=
v = {1, 2, 3, 4}
Out[1]=
{1, 2, 3, 4}
In[2]:=
totlen = 2*Length[v] - 1;
In[2]:=
w = Join[Table[0, {totlen - Length[v]}], Reverse[v],
Table[0, {totlen - Length[v]}]]
Out[3]=
{0, 0, 0, 4, 3, 2, 1, 0, 0, 0}
In[4]:=
Table[Take[w, {k, k + Length[v] - 1}], {k, totlen, 1, -1}]
Out[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}}
alan calvitti
systems & control engr
case western reserve university
"Jordan Rosenthal" <jr at ece.gatech.edu> writes:
> 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 ]