Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: VectorToMatrix
Replies: 6   Last Post: Jul 8, 1996 2:19 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Dr. T. L. Marchioro II

Posts: 14
Registered: 12/7/04
Re: VectorToMatrix
Posted: Jun 28, 1996 10:00 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Robert Pratt 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?


This proved a little more difficult than I had thought... but I think
the following is what you want to convert a vector into the elements of
an upper triangular matrix. I am a little surprised your example is not
nXn, but assume that is what you want. If not, the following should be
easy to adapt. No doubt some guru can present a more efficient method :)

vectorToUTMatrix[l_List, n_Integer]:=
Module[{temp, temp1},
(*make the first row *)

temp=RotateRight[Join[l, Table[0, {n}]]];
temp1=Take[temp, n+1];

(* now form other rows and join them to first *)
Do[(
temp=Drop[temp, {iter+1, n+1}];
temp=RotateRight[temp];
temp1=Join[temp1, Take[temp, n+1]];),
{iter, 1, n-1}];

(* Partition list of elements into matrix of proper shape *)
Partition[temp1,n+1]
]


For me this yields:

l={a, b, c, d, e, f, g, h, i, j};


vectorToUTMatrix[l,4]//MatrixForm

0 a b c d

0 0 e f g

0 0 0 h i

0 0 0 0 j


l=Join[l, {k, el, m, n, o}];


vectorToUTMatrix[l,5]//MatrixForm


0 a b c d e

0 0 f g h i

0 0 0 j k el

0 0 0 0 m n

0 0 0 0 0 o


l=Join[l, {p,q, r, s, t, u}];

vectorToUTMatrix[l,6]//MatrixForm

0 a b c d e f

0 0 g h i j k

0 0 0 el m n o

0 0 0 0 p q r

0 0 0 0 0 s t

0 0 0 0 0 0 u

etc. I hope this is what you were after.

TLM

--
Dr. Thomas L. Marchioro II Two-wheeled theoretical physicist
Applied Mathematical Sciences 515-294-9779
Ames Laboratory 515-432-9142 (home)
Ames, Iowa 50011 tlm@ameslab.gov
Project Coordinator: Undergraduate Computational Engineering and Sciences
http://uces.ameslab.gov/









Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.