Search All of the Math Forum:

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

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

 Messages: [ Previous | Next ]
 Dr. T. L. Marchioro II Posts: 14 Registered: 12/7/04
Re: VectorToMatrix
Posted: Jun 28, 1996 10:00 AM

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/

Date Subject Author
6/26/96 Robert Pratt
6/28/96 Demetri Mouratis
6/28/96 Dr. T. L. Marchioro II
7/8/96 Paul A. Rubin
7/8/96 Count Dracula
7/8/96 Nicholas Santucci
7/8/96 Paul Abbott