Message Boards Message Boards

0
|
8131 Views
|
11 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Create variables with subscripts automatically?

Posted 9 years ago

I'm currently working on a project and I have created by hand 20 variables with the form X [subscript i] [subscript j], and it is very tedious. I don't want to do this 80 or 100 times by hand. Is there any way easier than this? Thanks in advance and sorry for the bad English.

11 Replies
Posted 9 years ago

Perhaps

Table[Subscript[X, i, j], {i, 1, 10}, {j, 1, 10}]

and then scrape-n-paste as needed?

Be careful with subscripted variables because they are different from "ordinary" variables in some situations.

POSTED BY: Bill Simpson

If you use your variables for real computations (as opposed to math typesetting) the I would consider labeling variables differently, as @Bill Simpson recommended too. The easiest:

Array[a, {3, 3}]

{{a[1, 1], a[1, 2], a[1, 3]}, {a[2, 1], a[2, 2], a[2, 3]}, {a[3, 1], a[3, 2], a[3, 3]}}

then an example of computation is:

Solve[a[1, 2]^3 - a[1, 2] + 1 == 0, a[1, 2]];
TraditionalForm[Column[%]]

enter image description here

POSTED BY: Vitaliy Kaurov

If you want to do anything with the variables, indeed do not use Subscript, it will generally be very confusing and frustrating...

The new function

Indexed

might be useful as well...

POSTED BY: Sander Huisman

Thank you so much for answering so quickly. Your answer was the one I needed!

Thanks for the answer. Didnt know there would be such problems as I am merely starting

Thanks, will look into it. Didnt know there would be such problems with subscripts.

Not really a problem, more of a misconception. One should see Subscript merely as a typographical construct, nothing else. If one adheres to those confines generally there is no `problem'...

POSTED BY: Sander Huisman

It is meanwhile routine on this platform that questions come up on the use of, or problems (i.e. "unexpected behavior") with, Subscript. Sanders description of those as a "typographical construct" is just perfect!

Because in the beginning I had problems myself in proper understanding, I would like to give a little explanation:

If you have a variable (say a) and you make an assignment, that means that the variable gets an "ownvalue":

a = 3;
OwnValues[a]
(*  Out:  {HoldPattern[a]\[RuleDelayed]3}  *)

Whereas when you make the assignments:

enter image description here

there are no OwnValues at all! The point rather is that in a way you changed the properties of Subscript:

enter image description here

This is pretty much the same as if you write:

f[1] = 3;

There is no problem in doing this, but one would not want to call f[1] a "variable".

POSTED BY: Henrik Schachner

"Typographical construct" might indeed be a good addition to the documentation...

POSTED BY: Sander Huisman
Posted 9 years ago
POSTED BY: Bill Simpson
POSTED BY: Sander Huisman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract