Re: Package to 'graft' Lists onto Matrices

• To: mathgroup at smc.vnet.net
• Subject: [mg87740] Re: [mg87667] Package to 'graft' Lists onto Matrices
• From: Murray Eisenberg <murray at math.umass.edu>
• Date: Wed, 16 Apr 2008 05:06:12 -0400 (EDT)
• Organization: Mathematics & Statistics, Univ. of Mass./Amherst
• References: <200804150952.FAA25046@smc.vnet.net>
• Reply-to: murray at math.umass.edu

```Strictly speaking, there is no such thing as a matrix in Mathematica,
despite there being a function MatrixQ to test whether an object that
"can represent a matrix".  Rather, one can represents a matrix as a list
of lists, where the latter individual lists -- all having the same
length for a genuine matrix -- represent the rows of the matrix.  (Or
one can create an Array.)

Here's an example of such a list:

m = { {1,2,3,4}, {5,6,7,8} };
MatrixQ[m]
Dimensions[m]
True
{2,4}

This should not be confused with a special formatted form that you get
by acting upon m with MatrixForm:

mat = MatrixForm[m];
MatrixQ[m]
False

So what's wrong about using Append to join new rows to the matrix?

m = Append[m, {a,b,c,d}];
Dimensions[m]
{3,4}

What simpler thing could you possibly want to "graft" a new row to the
bottom of the matrix?  (Or to use Prepend to graft the new row at the
top of the existing matrix.)

And, to graft a new column to the left side of a matrix:

m = Join[m, {{Sin},{Cos},{Tan}},2];
Dimensions[m]
{3,5}

So despite the example in ref/Append, no MapThread is required to adjoin
a new column -- provided one is willing to first make that column a
genuine matrix, as I did above with the double nesting in
{{Sin},{Cos},{Tan}}.

Certainly joining an additional column now in Mathematica 6 is not as
direct as it was in earlier versions where, after loading the package
LinearAlgebra`MatrixManipulation`, you could just use AppendRows:

a = Partition[Range[8],4];
AppendRows[a, {{Exp}, {Log}}]

Of course you can still load that legacy package and use AppendRows, but
who knows how much longer that package will be around?

Notice that, even with AppendRows, you still have to create an actual
matrix in order to stick new columns on -- just as you have to do with
Join[..., ..., 2].  (At least you can avoid worrying about "level".)

Should it be simpler.  I'm not sure there's a good way without breaking
something in the fundamental structure of Mathematica, or without
introducing a new, special construct of a matrix.  But that would
complicate lots of other things, I think.

robert prince-wright wrote:
> I've been looking at Join[ ], Append[ ], Insert[ ], etc. and wonder if someone has written a package that allows an Mathematica user to add Lists as rows and columns to form a new matrix.
>
>   It seems a little daft that that you need to use MapThread and Append to simply graft a row onto an existing matrix!
>
>   Am I missing some new functionality?
>
>
>
>
> Robert Prince-Wright
> Houston
> TX, 77006
> USA
>
>

--
Murray Eisenberg                     murray at math.umass.edu
Mathematics & Statistics Dept.
Lederle Graduate Research Tower      phone 413 549-1020 (H)
University of Massachusetts                413 545-2859 (W)
710 North Pleasant Street            fax   413 545-1801
Amherst, MA 01003-9305

```

• Prev by Date: Re: A kernel, multiple notebooks, and Global?
• Next by Date: Re: pdf and accents - SOLUTION FROM MYSELF
• Previous by thread: Re: Package to 'graft' Lists onto Matrices
• Next by thread: Re: Package to 'graft' Lists onto Matrices