0
$\begingroup$

I would like to define a vector with variables that can be inputed later. For instance, I would want to define some vector v = {v1, v2}, use it in some operations and then, much later, tell it what v1 and v2 are. How can I do this?

As an analogy, I could define a function f[x_,y_]:=2x+y and later tell mathematica what x and y are. But when I try to do the natural analogue for a vector---which I would have guessed is v[v1_,v2_]:= {v1, v2}---I get errors.

(I am sure this must be very basic, but I cannot find the right article.)

$\endgroup$
1
  • 3
    $\begingroup$ Try Clear[v]? $\endgroup$
    – MelaGo
    Jan 30, 2020 at 2:10

1 Answer 1

3
$\begingroup$

I found that you missed the SetDelayed.

v[a_, b_] := {a, b}

If you evaluate v[1, 2], you could get {1, 2}.

$\endgroup$
3
  • $\begingroup$ Sorry, I must be misunderstanding what you are saying. When I copied and pasted what you wrote, I got an error. When I googled, I kept getting SetDelayed is :=. I certainly inadvertently wrote "=" instead of ":=" in my original post, but I meant ":=" and have been getting errors with v[v1_,v2_]:= {v1, v2}. But probably I am misunderstanding what you mean. $\endgroup$
    – Amanda
    Jan 30, 2020 at 1:48
  • $\begingroup$ @Amanda Can you please try the following? First Clear[v]. Then v[a_, b_] := {a, b} and finally v[1, 2]. The result after the final command is {1,2} on my laptop without any errors $\endgroup$
    – user49048
    Jan 30, 2020 at 2:21
  • $\begingroup$ Ah, yes, when I clear it does work! Thanks! $\endgroup$
    – Amanda
    Jan 30, 2020 at 4:27

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.