MathGroup Archive 1996

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

Search the Archive

Re: VectorToMatrix


In article <4qqn5d$49m at dragonfly.wolfram.com>, Robert Pratt
<rpratt at math.unc.edu> wrote:

> I want to define a function VectorToMatrix[list_, n_] that behaves as 
> follows:
> 
> VectorToMatrix[{a,b,c,d,e,f,g,h,i,j}, 4]//MatrixForm
> 
> 0 a b c d
> 0 0 e f g
> 0 0 0 h i
> 0 0 0 0 j
> 
> Now n can be determined from Length[list], which will always be equal to 
> n choose 2 for some n.  But I would rather input n to save that 
> computation.  (I will be doing this for a lot of vectors.)
> 
> Any ideas?
> 
> Rob Pratt
> Department of Mathematics
> The University of North Carolina at Chapel Hill
> CB# 3250, 331 Phillips Hall
> Chapel Hill, NC  27599-3250
> 
> rpratt at math.unc.edu

How about this:

In[1]:=
Clear[VectorToMatrix]
VectorToMatrix[list_,n_,step_:1,outlist_:{}]:=
Module[{wlist,newlist},

wlist=Table[{1,i},{i,n,1,-1}];

newlist=Append[outlist,
   Flatten[
   List[Table[0,{step}],
       Take[list,wlist[[step]]] ]]];

If[step==n,
   newlist,
   VectorToMatrix[
      Drop[list,wlist[[step]]],
      n,
      step+1,
      newlist]]
      
];
In[3]:=
Clear[n]
n=4;
l=Table[FromCharacterCode[97+i],{i,0,n^2/2+n/2-1}];
MatrixForm[VectorToMatrix[l,n]]
Out[6]//MatrixForm=
0   a   b   c   d

0   0   e   f   g

0   0   0   h   i

0   0   0   0   j
In[7]:=
Clear[n]
n=6;
l=Table[FromCharacterCode[97+i],{i,0,n^2/2+n/2-1}];
MatrixForm[VectorToMatrix[l,n]]
Out[10]//MatrixForm=
0   a   b   c   d   e   f

0   0   g   h   i   j   k

0   0   0   l   m   n   o

0   0   0   0   p   q   r

0   0   0   0   0   s   t

0   0   0   0   0   0   u
-------------------------------------------------------------------
Demetri Mouratis
dmourati at uiuc.edu
http://www.wolfram.com/~demetrim/

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: VectorToMatrix
  • Next by Date: Re: distance learning
  • Previous by thread: Re: VectorToMatrix
  • Next by thread: Re: VectorToMatrix