0
$\begingroup$

I'm trying to do the following. I have a table, call it TransTable. To give an example

TransTable = {{0., 6.9, 7., 7.1, 13.9, 14., 14.1, 21.}, {0., 0., 0.100005, 0.20001,
   7., 7.1, 7.20001, 14.1}, {0., 0., 0., 0.100005, 6.9, 7., 7.1, 
  14.}, {0., 0., 0., 0., 6.79999, 6.9, 7., 13.9}, {0., 0., 0., 0., 0.,
   0.100005, 0.20001, 7.1}, {0., 0., 0., 0., 0., 0., 0.100005, 
  7.}, {0., 0., 0., 0., 0., 0., 0., 6.9}, {0., 0., 0., 0., 0., 0., 0.,
   0.}}

What I want to do is as follows. This table has dimensions 8 by 8, or in general x by y. I want to extract all nonzero values, call them z_xy, and define variables according to

Varyx = z_xy

So for example Var21 = -6.9 should follow from this.

Now, I've had a look around and I think some of the ingredients are contained in the answer to Get positions of all non zero matrix elements however I don't really see how to adapt this into naming the variables. Clearly NonzeroPositions can be used to find the positions, but the naming step would then not be obvious to me.

$\endgroup$
2
$\begingroup$
Function[{n, m},
   Evaluate[
     ToExpression["Var" <> IntegerString[m] <> IntegerString[n]]] = 
    TransTable[[n, m]]] @@@ Position[Abs@TransTable, _?Positive];

Now you have the variables defined,

Var21
Var83
(* -6.9 *)
(* -14. *)

Of course, this isn't usually the best practice. If any of the variables already has a name, then this will throw an error, so you could prepend it with a call to ClearAll,

Function[{n, m},
   ClearAll /@ Names["Var" <> IntegerString[m] <> IntegerString[n]];
   Evaluate[
     ToExpression["Var" <> IntegerString[m] <> IntegerString[n]]] = 
    TransTable[[n, m]]] @@@ Position[Abs@TransTable, _?Positive];

which can be called over and over again.

Usually, there isn't any reason not to use indexed variables, so instead of Var21, you would have Var[2,1]. This makes the code much easier,

Set[Var[#2, #1], TransTable[[#1, #2]]] & @@@ 
  Position[Abs@TransTable, _?Positive];

Either way, it is easy to print the list of variables that have been assigned,

Names["Var*"]
(* {"Var", "Var21", "Var31", "Var32", "Var41", "Var42", 
"Var43", "Var51", "Var52", "Var53", "Var54", "Var61", "Var62", 
"Var63", "Var64", "Var65", "Var71", "Var72", "Var73", "Var74", 
"Var75", "Var76", "Var81", "Var82", "Var83", "Var84", "Var85", 
"Var86", "Var87", "Variables", "Variance", "VarianceEquivalenceTest", 
"VarianceEstimatorFunction", "VarianceGammaDistribution", 
"VarianceTest"} *)

for the first method, or for the indexed method,

?Var

Global`Var

Var[2,1]=-6.9

Var[3,1]=-7.

Var[3,2]=-0.100005

Var[4,1]=-7.1

Var[4,2]=-0.20001

Var[4,3]=-0.100005

Var[5,1]=-13.9

Var[5,2]=-7.

Var[5,3]=-6.9

Var[5,4]=-6.79999

Var[6,1]=-14.

Var[6,2]=-7.1

Var[6,3]=-7.

Var[6,4]=-6.9

Var[6,5]=-0.100005

Var[7,1]=-14.1

Var[7,2]=-7.20001

Var[7,3]=-7.1

Var[7,4]=-7.

Var[7,5]=-0.20001

Var[7,6]=-0.100005

Var[8,1]=-21.

Var[8,2]=-14.1

Var[8,3]=-14.

Var[8,4]=-13.9

Var[8,5]=-7.1

Var[8,6]=-7.

Var[8,7]=-6.9

$\endgroup$
  • $\begingroup$ That's very nice. I'm just thinking of this now, but perhaps I could add an option that prints which variables have been assigned? I think I should be able to add some conditional to the above right? $\endgroup$ – user129412 Mar 15 '16 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.