Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Clear subscripted variables

104 views
Skip to first unread message

Mukhtar Bekkali

unread,
Jun 25, 2005, 2:15:16 AM6/25/05
to
Here is the code (x\_i stands for x subscript i):

Table[{Print[x\_i = i]; Clear[x]; Remove[x]; Unset[x]; Print[x\_i]},
{i, 1, 2}]

I expected that x\_i is cleared at the end of the loop but it isn't

If I insert Clear[Subscript] in the loop then it is cleared, but not
desirable since it clears all variables with subscripts.

What command shall I use to clear specific subscripted variables?

Mukhtar Bekkali

Pratik Desai

unread,
Jun 26, 2005, 1:41:16 AM6/26/05
to
Mukhtar Bekkali wrote:

Hi,
Here is what I think the problem is
Why is x equal to x\_i?
Even if it is, how can mathematica know it

I think what you can try is
Table[Subscript[x, i] = 1, {i, 1, 2}]
(*Table[Unset[Subscript[x, i]], {i, 1, 2}]
Table[Subscript[x, i], {i, 1, 2}]*)

You can uncomment the unset table to clear the variables, or better yet
write a module to do it (i.e Unset the subscipted variables)

Here is a rough version of the same

unsetmod[x_, list_] := Module[{list1}, list1 =
list; Table[Unset[Subscript[x, list1[[s]]]], {s, 1, Length[list]}]]

Table[Subscript[x, i] = 1, {i, 1, 5}]
(*unsetmod[x, Range[2]]*)
Table[Subscript[x, i], {i, 1, 5}]

Best regards
Pratik

--
Pratik Desai
Graduate Student
UMBC
Department of Mechanical Engineering
Phone: 410 455 8134


David Bailey

unread,
Jun 26, 2005, 1:42:47 AM6/26/05
to
Hi,

Try this:

Table[a\_i =. , {i, 1, 10}]

This is equivalent to the use of Unset[a\_i]

David Bailey
http://www.dbaileyconsultancy.co.uk

Jean-Marc Gulliet

unread,
Jun 26, 2005, 1:44:18 AM6/26/05
to
Hi Mukhtar,

Subscripted variables are not symbols in Mathematica: *Clear* and
*Remove* complain about that whereas *Unset* works.

You can use the package *Symbolize* to more or less transform
subscripted variables into symbols. For example,

In[1]:=
\!\(x\_1 = 1\)
Out[1]=
1
In[2]:=
\!\(x\_1\)
Out[2]=
1
In[3]:=
\!\(Clear[x\_1]\)
From In[3]:=
\!\(\*
RowBox[{\(Clear::"ssym"\), \(\(:\)\(\ \)\), "\<\"\\!\\(x\\_1\\) is
not a \
symbol or a string. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\",
ButtonStyle->\
\\\"RefGuideLinkText\\\", ButtonFrame->None,
ButtonData:>\\\"Clear::ssym\\\"]\
\\)\"\>"}]\)
In[4]:=
\!\(Remove[x\_1]\)
From In[4]:=
\!\(\*
RowBox[{\(Remove::"ssym"\), \(\(:\)\(\ \)\), "\<\"\\!\\(x\\_1\\) is
not a \
symbol. \\!\\(\\*ButtonBox[\\\"More\[Ellipsis]\\\", \
ButtonStyle->\\\"RefGuideLinkText\\\", ButtonFrame->None, \
ButtonData:>\\\"Remove::ssym\\\"]\\)\"\>"}]\)
In[5]:=
\!\(FullForm[HoldForm[x\_1]]\)
Out[5]//FullForm=
HoldForm[Subscript[x,1]]
In[6]:=
\!\(Unset[x\_1]\)
In[7]:=
\!\(x\_1\)
Out[7]=
\!\(x\_1\)
In[8]:=
Needs["Utilities`Notation`"]
In[9]:=
\!\(\*
RowBox[{"Symbolize", "[",
TagBox[\(x\_1\),
NotationBoxTag,
TagStyle->"NotationTemplateStyle"], "]"}]\)
In[10]:=
\!\(x\_1 = 2\)
Out[10]=
2
In[11]:=
\!\(Clear[x\_1]\)
In[12]:=
\!\(x\_1\)
Out[12]=
\!\(x\_1\)

Hope this helps,
/J.M.

Jens-Peer Kuska

unread,
Jun 28, 2005, 5:18:37 AM6/28/05
to
Hi,

ClearSubscript[x_Symbol] := (First /@
Select[DownValues[Subscript],
MatchQ[First[#],
Verbatim[HoldPattern][Subscript[x, _]]] &]) /.
HoldPattern :> Unset

and

ClearSubscript[x] will remove all Subscript[x,_]
values.

Regards

Jens

"Mukhtar Bekkali" <mbek...@gmail.com> schrieb im
Newsbeitrag news:d9ispk$cpd$1...@smc.vnet.net...

0 new messages