Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

after 6 hours reading about string patterns, string manipulations and trying ..and trying..I officially give up (for now) and ask for some help.

I have some unorderly equations you will find here

Note: I only have 6 equations shown here, but actually it's 50 equations.

I want to 1) extract the coefficients of every equation and 2) put them in a 50x50 matrix (to get a nice coefficient matrix for this linear system) on the right position, which is indicated by the number behind the name of the variable. For example:

ba5 = 255.587802741733 + 0.416407328023007*ba4 + 0.347918861182194*ba7 + 0.256752019528632*ba42 + 0.142629613367709*ba38 - 0.153600910218342*ba27 - 0.157696503802691*ba39

ba5 means that I want to fill the 5th row of the matrix. ba4 is the 4th column, ba7 is the 7th column, ba42 is the 42th. So in the 4th column the coefficient 0.4164... should be copied and in the 7th the coefficient 0.3479..and so on.

As another example look at the second equation:

ba6 = 0.387399976154608*ba8 + 0.343433662895965*ba4 + 0.17517619838171*ba7 + 0.116548914112818*ba44 - 6.80424536420765

Here it is row 6 I want to fill. But this time it should copy the coefficients to the 8th(ba8), 4th(ba4),7th(ba7) and 44th(ba44) column.

Additionally I need to copy the independent variable (in this case ba5 and ba6) on the right side of the equation and the constant to the left side to get a system in the form of A*x=y with A as my 50x50 matrix, x as my vector of ba's (ba4, ba5,ba7 asf) and y as my constants (255.587 in the first equation and 6.80 in the second).

Right now, my brain is a little bit messed up and I hope you guys can understand what I mean. If not, please ask.

EDIT: You can download all equations here

Note: They are not continuously numbered and it's 41 equations, my bad :/

EDIT: I tried it like this, but I get some error messages:

   ToExpression[equations[[x]]], {Ba1, ba2, ba3, ba4, ba5, ba6, ba7, 
    ba8, ba9, ba10, ba13, ba17, ba18, ba19, ba20, ba21, ba22, ba23, 
    ba24, ba25, ba27, ba28, ba29, ba30, ba31, ba32, ba33, ba34, ba35, 
    ba36, ba37, ba38, ba39, ba40, ba41, ba42, ba43, ba44, ba45, ba46, 
    ba47}], {x, 1, 41}]


I guess it's because ToExpression assigns a value to my Ba's. The same which I use in the list of CoefficientArrays. But I can't use this function when I'm reading strings :/

share|improve this question
possible duplicate of Extract matrix from a system of linear equations – Szabolcs Feb 4 '14 at 18:23
I think this is a duplicate of the question linked above, with the addition that you should use = in the equations. Instead, have the right hand side of each of them (corresponding to ba1, ba2, ... respectively) in a list. Then use that list, together with the list of symbols {ba1, ba2, ...} in the method described in the linked answer. – Szabolcs Feb 4 '14 at 18:24
Thanks @Szabolcs, the link helps me out. But what about the constant, i.e the number without a variable. In every equation it is on another position. Somehow I have to isolate this one and put it on the other side of the equation, or a least isolate it. – holistic Feb 4 '14 at 18:47
If you will include your full list of equations I will provide you with a method to do what I believe you are asking for. – Mr.Wizard Feb 4 '14 at 19:22
@holistic The answer I linked is still usable for your purposes. Suppose your set of equations is $x=x+y+1$ and $y=x-y+2$. Then the command to use is CoefficientArrays[{x + y + 1, x - y + 2}, {x, y}] which gives a list of the form {b,a}. b are the constants and a is the coefficient matrix. – Szabolcs Feb 4 '14 at 21:16
up vote 1 down vote accepted

There are complications to this question (more specifically your data) that are not present in the proposed duplicate, so I wish to address them. First, your data uses Set so e.g. ba5 will be assigned a value, and every time this Symbol appears that value will be substituted. We need to prevent that, so first I will Import the data in a held form, then replace Set with Rule for later manipulation.

data = Import[
   {"Package", "HeldExpressions"}];

data2 = data /. Set -> Rule // ReleaseHold;

We then need to extract all the variables. Although in this case you have in order a line for each variable that appears in the polynomials let me show you how to order these otherwise. That will also let me create a mapping between Symbol names and their numeric labels.

vars = Variables[Last /@ data2];

nums = ToExpression @@@ StringCases[SymbolName /@ vars, NumberString]
{32, 35, 36, 1, 10, 13, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 27, 28, 29, 3, 30,
 31, 33, 34, 37, 38, 39, 4, 40, 41, 42, 43, 44, 45, 46, 47, 5, 6, 7, 8, 9}
varsOrdered = vars[[Ordering @ nums]];
{Ba1, Ba2, ba3, ba4, ba5, ba6, ba7, ba8, ba9, ba10, ba13, ba17, ba18, ba19, ba20, ba21,
 ba22, ba23, ba24, ba25, ba27, ba28, ba29, ba30, ba31, b32, ba33, ba34, b35, b36, ba37,
 ba38, ba39, ba40, ba41, ba42, ba43, ba44, ba45, ba46, ba47}

I am not certain of the format of the array that you want as your variable names go up to 47 yet there are gaps (as you comment there are 41 total). Also, I shall make assumptions about how you want the constant values (e.g. 255.587802741733) handled. Nevertheless, we are ready to proceed to extracting the coefficients:

arrays = CoefficientArrays[varsOrdered /. data2, varsOrdered]

The constants are in the first array:

arrays[[1]] // Normal // Short
{-30.2067, 38.3391, 94.8379, <<35>>, 118.776, -12.5049, -50.0722}

The second array is the main output that you want. It is a 41x41 array with no gaps for "missing" variable numbers such as 11, and 12. You can display it with the numeric labels that we extracted above as follows:

result = Join[List /@ #, #2, 2] & @@ arrays;

 TableForm[result, TableHeadings -> {Sort@nums, Prepend[Sort@nums, "C"]}],
 Scrollbars -> True

enter image description here

share|improve this answer
I really am glad to have joined this forum, I can learn a lot, thank you very much!! The uncontinuous numbering is because the numbers represent so called Brodmann brainareas (e.g. 17 is the visuell cortex) and 11,12 were left out in my other programm I used to generate the equations. So your assumptions are helpful, because they keep the "natural numbering" This made it much more tricky. Thanks again :) – holistic Feb 5 '14 at 13:17
@holistic You're welcome. – Mr.Wizard Feb 5 '14 at 13:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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