MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Converting list of subscripted variables into an array

  • To: mathgroup at smc.vnet.net
  • Subject: [mg118314] Re: Converting list of subscripted variables into an array
  • From: Bill Rowe <readnews at sbcglobal.net>
  • Date: Thu, 21 Apr 2011 03:11:55 -0400 (EDT)

On 4/20/11 at 4:27 AM, cy56 at comcast.net (Christopher O. Young) wrote:

>I have a long list that's supposed to represent points on 9
>different curves. I'd like to put them into an array C[j,k] where j
>is the index for the curves and k is the index for the points.

>Subscript[c1, 1] = {1.796875, 1.34375}

>Subscript[c1, 2] = {2.15625, 1.34375}

>Subscript[c1, 3] = {2.296875, 1.359375}

>Subscript[c1, 4] = {2.375, 1.46875}

>Subscript[c1, 5] = {2.46875, 1.671875} ......

>I'm new at pattern matching, and I'm having a hard time getting
>started here. Can anyone steer me in the right direction?

It is difficult to know what to tell you. You ask about pattern
matching but show code with strings which really isn't the same.
In Mathematica, part of an array c is accessed as c[[i,j]]. The
notation c[i,j] is the function c with arguments j,k. At times,
it is convenient to thick of c[j,k] as an array c indexed by j,k
but it is really different.

For example

In[4]:= c = RandomReal[1, {3, 3}];

In[5]:= ArrayQ[c]

Out[5]= True

In[6]:= x[1, 1] = 1; x[1, 2] = 2; x[2, 1] = 3; x[2, 2] = 4;

In[7]:= ArrayQ[x]

Out[7]= False

And for functions that have the property LIstable such as Sqrt,
Sqrt[c] returns an array where each of the elements of is the
square root of the corresponding element of c. But Sqrt[x]
returns unevaluated.

Also note, I used a lower case c not an upper case c. You cannot
use an upper case C since this already has built-in meaning. In
general, it is a bad idea to use single upper case letters as
variables due to conflicts with built-in objects.

>Chris Young cy56 at comcast.net IntuMath.org

>In[180]:= "c" <> IntegerString[1]
>Out[180]= "c1"

>In[181]:= Subscript["c" <> IntegerString[1], 2]
>Out[181]= Subscript["c1", 2]

>In[182]:= Evaluate[%]

Instead do either:

In[1]:= Subscript[Evaluate@ToExpression["c1"], 1]

Out[1]= Subscript[c1, 1]

or

In[2]:= ToExpression["Subscript[c1,1]"]

Out[2]= Subscript[c1, 1]

Also note, while you can do

In[3]:= Subscript[c1, 1] = {2, 3}; Subscript[c1, 1]

Out[3]= {2,3}

Clear[Subscript[c1,1]]

fails with an error message. You can make subscripted variables
in Mathematica that will behave just like unsubscripted
variables by using the Notation package (see
Notation/guide/NotationPackage in the Documentation Center).
=46ailure to use the Notation package will result in behavior you
probably won't be expecting. Frankly, I see the effort required
to make subscripted variables work nicely in Mathematica to be
more trouble than it is worth and rarely use subscripted variables.



  • Prev by Date: Re: SortBy + Sort Strings with apo.marks + CharacterCode
  • Next by Date: Re: Simplify results
  • Previous by thread: Re: Converting list of subscripted variables into an array
  • Next by thread: Re: Converting list of subscripted variables into an array