1
$\begingroup$

I want to define a vector PosVec[a, b] dependent on the variables a and b of a matrix VecMat[a, b]. I want to calculate the "positions" in VecMat[a, b] of the maximum values in VecMatQ[a, b] and store them in PosVec[a, b] .

My problem is that, with my code below, I can't access the variables a and b of PosVec by PosVec[a, b] -> PosVec[1, 1] For the positions in VecMatQ where there are entries of a and b, Position generates empty spaces as shown below:

Here is my code:

Remove["Global`*"]

m = {{1, -a + b, 3}, {b, -6, 4}, {3, b, 0}};

VecMat[a_, b_] = Eigenvectors[m];

VecMatQ[a_, b_] = VecMat[a, b]*Conjugate[VecMat[a, b]];

PosVec[a_, b_] = 
  Table[Position[VecMatQ[a, b][[i]], Max[VecMat[a, b][[i]]]], {i, 1, 3}] // Flatten;

PosVec[1, 1]

{{},{},{}}

but

Table[Position[VecMatQ[1, 1][[i]], Max[VecMat[1, 1][[i]]]], {i, 1, 3}] // Flatten

gives the right values

{(3),(2),(1)}

Can someone help me to make the evaluation of Position dependent on the variables a and b?

$\endgroup$
4
  • 2
    $\begingroup$ Shortest way: change PosVec[a_, b_] = to PosVec[a_, b_] :=, i.e. use SetDelayed instead of Set. Longest (but cleaner I think) way: change all Set to SetDelayed, define m as a two-args function, and modify the right-hand side of your second definition accordingly. $\endgroup$
    – user31159
    Commented May 24, 2016 at 18:31
  • $\begingroup$ Is it essential if I define m as two arg function or I do it after calculation of Eigenvectors? $\endgroup$
    – MathNut
    Commented May 25, 2016 at 13:51
  • $\begingroup$ I think it depends on what you are doing. I would recommend in any case using SetDelayed instead of Set when for instance a definition involves parameters (so regarding m, defining it as m := {{1, -a + b, 3}, {b, -6, 4}, {3, b, 0}}). If a and b were to have values before defining m, the definition of m won't be affected, which is not the case when using a Set. $\endgroup$
    – user31159
    Commented May 25, 2016 at 14:28
  • $\begingroup$ Probably of interest about Set and SetDelayed is this post. $\endgroup$
    – user31159
    Commented May 25, 2016 at 14:31

1 Answer 1

1
$\begingroup$

Shortest way: change PosVec[a_, b_] = to PosVec[a_, b_] :=, i.e. use SetDelayed instead of Set. Longest (but cleaner I think) way: change all Set to SetDelayed, define m as a two-args function, and modify the right-hand side of your second definition accordingly. – Xavier 19 hours ago

Yes that works thx!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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