Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

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

If I have a vector of the form (xp+y, x+yp) do you have a simple way of creating the following matrix and vector from it: {{p, 1}, {1, p}} * {x, y}, such that after multiplying the matrix with the vector, I get the original vector back.

If I have the following variables defined as such:

v1 = (a1 + 1) *(b1 + 1) *(c1 + 1)*(d1 + 1) *(e1 + 1)  -1

a1 = (x11*f11 + x21*f21 )

b1 = (x11*f12 + x21*f22 )

c1 = (x11*f13 + x21*f23 )

d1 = (x11*f14 + x21*f24)

e1 = (x11*f15 + x21*f25)

is there a fast way to convert all the numbers appearing next to letters for example 11 next to x in to subscript? My goal is to create a vector consisting of several elements that look like V1.

share|improve this question
Ad 1): does your vector consist of two numbers and do you want to solve for x, y, and p? Or does your vector consist of variables x, y, and p as shown and do you want to perform a structural rearrangement? – Sjoerd C. de Vries Aug 10 '13 at 7:07

1) maybe something like that:

A = (Transpose@{# /. {x -> 1, y -> 0}, # /. {x -> 0, y -> 1}}) &[{x a11 + a12 y, a21 x + y a22} ]

where the brackets at the end contain the vector with the equations with the variables x and y that you want to write as a matrix.

2) When doing computations, I experienced odd behavior when using subscripts as variables, but in principle you can use regular expressions and string replacement to get subscripts. (but I would rather use this only for text output like labels)

ToExpression@StringReplace["v2=(a11+1)*(b1+1)", RegularExpression["([a-z])([0-9]+)"] -> "Subscript[$1,$2]"] 
share|improve this answer

Regarding the second part of your question this may be either a formatting issue or one of part extraction. I'll address both.

For formatting alone you might use something like this:

v1 /. s_Symbol /; StringMatchQ[SymbolName[s], _ ~~ NumberString] :> 
  Subscript @@ Characters @ SymbolName @ s

enter image description here

Note that the elements of the Subscript expression are strings. For basic formatting this should not be a problem, but if it is you can use:

v1 /. s_Symbol /; StringMatchQ[SymbolName[s], _ ~~ NumberString] :>
  (HoldForm @@@ ToHeldExpression /@ Subscript @@ Characters @ SymbolName @ s)

If you want not merely formatting but instead something like part extraction from a separate matrix x or f then you could use this:

x = {{q}, {r, s}};
f = {{w1, w2, w3, w4, w5}, {x1, x2, x3, x4, x5}};

v1 /. s_Symbol /; StringMatchQ[SymbolName[s], _ ~~ NumberString] :>
  (Part @@ Join @@ ToHeldExpression /@ Characters @ SymbolName @ s)

-1 + (1 + q w1 + r x1) (1 + q w2 + r x2) (1 + q w3 + r x3) (1 + q w4 + r x4) (1 + q w5 + r x5)

The function Part could be replaced with another if you have a different transformation in mind.

share|improve this answer
HI Thank you for you help. I was hoping to create a vector whose elements are EXPAND[vi] (the first subscript of x would change accordingly). Once I have this vector I wanted to make a matrix of coefficients from all the xii and a vector from all the fii so when i multiply XF = V . – user8968 Aug 10 '13 at 17:10

Concerning the first question I would simply use Mathematica's Matrix Multiplication. This is done by using a dot. For your example:

{{p, 1}, {1, p}}.{x, y}

yields to

{p x + y, x + p y}

Here is an attempt to solve also the opposite problem. First I form a 2x2 matrix by replacing the Plus.

matrix1 = List @@ # & /@ {p x + y, x + p y}

Then I look for elements which are a part of all the rows within a certain column.

vector = MapThread[(Intersection[{List @@ #1} // 
   Flatten, {List @@ #2} // Flatten]) &, matrix1] // Flatten

And the last thing I do is a divison of each row with the result vector and I as a solution I get the matrix.

(#/vector) & /@ matrix1

This is just a try I'm sure there is a better much more universal solution possible.

share|improve this answer
It is my understanding of the question that he wants to go the other way: start with {p x + y, x + p y} and get (held, presumably) {{p, 1}, {1, p}}.{x, y} – Mr.Wizard Aug 10 '13 at 8:02
Mr. Wizard is correct! – user8968 Aug 10 '13 at 17:15
Thank you. Now I get it. Posted a possibility also for this problem. – RMMA Aug 10 '13 at 18:25

CoefficientArrays[{xp+y,x+py},{x,y}] yields the matrix.

share|improve this answer
I presume you mean something like CoefficientArrays[{x p + y, x + p y}, {x, y}][[2]] // Normal (Normal for presentation)? – Mr.Wizard Aug 12 '13 at 13:55
@Mr.Wizard yes. I should have suggested this as a comment for obtaining the desired matrix and the obviously there are no constant terms so the first element will be {} and Normal makes for display. I must admit to confusion about the question. It appears to be two questions and I remain uncertain as to the second question. – ubpdqn Aug 13 '13 at 6:52

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.