The Math Forum

Search All of the Math Forum:

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

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

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

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

> 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 *)
temp=Drop[temp, {iter+1, n+1}];
temp1=Join[temp1, Take[temp, n+1]];),
{iter, 1, n-1}];

(* Partition list of elements into matrix of proper shape *)

For me this yields:

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


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}];


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}];


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.


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

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

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.