MathGroup Archive 2000

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Could this be improved?


Jordan Rosenthal schrieb:
> 
> 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

Hi Jordan,

I suggest

In[1]:= v = {1, 2, 3};

In[2]:=
With[{r = Length[v] - 1}, 
    Transpose[NestList[RotateRight, Join[v, Table[0, {r}]], r]]] //
TableForm

-- Hartmut


  • Prev by Date: Re: magic pen
  • Next by Date: Sturm-Liouville Problem (differential equation eigenvalue problem)
  • Previous by thread: Could this be improved?
  • Next by thread: Re: Could this be improved?