Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2000
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2000

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

Search the Archive

Re: Could this be improved?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg21769] Re: [mg21741] Could this be improved?
  • From: Bojan Bistrovic <bojanb at physics.odu.edu>
  • Date: Thu, 27 Jan 2000 22:56:50 -0500 (EST)
  • Sender: owner-wri-mathgroup at wolfram.com

> 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
> 
Here's one:

myMtx[v_List]:=
  Table[Take[
      Drop[Join[Table[0,{kk,1,2  Length[v]-jj}],Reverse[v],
          Table[0,{kk,1,jj}]],Length[v]-1],Length[v]],{jj,2Length[v] ,2,-1}]

or this one (which is faster)

myMtx2[v_List]:=
  Join[Table[
      Table[ PadRight[Take[Reverse[v],-kk],Length[v]],{kk,1,Length[v]}]],
    Reverse[Table[
        Table[PadLeft[Take[Reverse[v],kk],Length[v]],{kk,1,Length[v]-1}]]]]

Bye, bojan

-------------------------------------------------------------
Bojan Bistrovic,                       bojanb at physics.odu.edu  
Old Dominion University, Physics Department,      Norfolk, VA
-------------------------------------------------------------


  • Prev by Date: Re: Simplifying Finite Sums With A Variable # of Terms
  • Next by Date: Re: Extended Random Function
  • Previous by thread: Re: Could this be improved?
  • Next by thread: Re: Could this be improved?