0
$\begingroup$

I have a list of complicated expressions that involve indexed coefficients. For example:

expr={m[5],c[3]*m[1]^2+m[2],Sqrt[m[2]+3*m[3]*c[8],foo[m[3]*m[5]]};

How can I find the largest m index that appears in each list element? The outpoot I'd like to see is:

maxIndex[expr]

(*  {5,2,3,5}   *) 

Note that the m[q_]'s might appear inside functions, such as Sqrt[] and foo[].

$\endgroup$
  • $\begingroup$ Your expr has a typo; please check it. $\endgroup$ – Mr.Wizard Aug 3 '15 at 7:22
3
$\begingroup$

Probably what you want:

expr = {m[5], c[3]*m[1]^2 + m[2], Sqrt[m[2] + 3*m[3]*c[8]], foo[m[3]*m[5]]};

Max @ Cases[#, m[n_] :> n, {0, -1}] & /@ expr
{5, 2, 3, 5}

With an operator form of Map:

maxIndex = Map[Max @ Cases[#, m[n_] :> n, {0, -1}] &];

maxIndex[expr]
{5, 2, 3, 5}
$\endgroup$
  • $\begingroup$ Perfection! Thank you. $\endgroup$ – Jerry Guern Aug 3 '15 at 10:32

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.