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 ]
 Paul A. Rubin Posts: 397 Registered: 12/7/04
Re: VectorToMatrix
Posted: Jul 8, 1996 1:54 AM

In article <4qqn5d\$49m@dragonfly.wolfram.com>,
Robert Pratt <rpratt@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@math.unc.edu

Another possible solution to this uses the MatrixManipulation standard
package:

<<LinearAlgebra`MatrixManipulation`
utmatrix[ x_List, n_Integer ] :=
Module[
{ a, b, c },
a = Drop[ UpperDiagonalMatrix[ b, n + 1 ], 1 ];
c = DeleteCases[ Flatten[ a ], 0 ];
a /. Thread[ Rule[ c, x ] ]
] /; Positive[ n ] && (Length[ x ] == n(n + 1)/2)
utmatrix[ {a,b,c,d,e,f,g,h,i,j}, 4 ] // MatrixForm

Paul

**************************************************************************
* Paul A. Rubin Phone: (517) 432-3509 *
* Department of Management Fax: (517) 432-1111 *
* Michigan State University *
* East Lansing, MI 48824-1122 (USA) *
**************************************************************************
Mathematicians are like Frenchmen: whenever you say something to them,
they translate it into their own language, and at once it is something
entirely different. J. W. v. GOETHE

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