Community Tip - When posting, your subject should be specific and summarize your question. Here are some additional tips on asking a great question. X
"This value must be a scalar"
Why does the standard lognormal (Pr_lnorm) work
but when I introduce a program into the function definition (Pr_tlnorm), I get the above error?
Solved! Go to Solution.
If you want to call a function which, like yours, expects a scalar argument "id" using a vector instead and expect this vector to be fed element by element as argument of the function and the various results to be collected in a result vector, you have to use vectorization!
... or
if you expect only vectors are arrays as input for id..
There should be no difference regarding the result you get. The solution of JKT works OK because vectorization doesn not complain if no vector is involved at all. So the function S.tr can be called with a vector as argument as well as with a single scalar.
Personally I find it more clear to set up my functions to work with scalars only and use explicit vectorization only when I call those function with a vector as argument. That way its always clear that I am nor using a vector function but a scalar function which is evaluated for the set of scalar values in the vector argument. Personally I feel that that clearness justifies the extra effort of having to explicitly apply vectorization when the function is called with a vector argument.
But I also understand the argument concerning convenience in case of JKT's approach where vectorization has to be applied only once at definition stage. The choice is up to you and you should experience no problems regardless which approach you prefer.
Thanks for the clarification. In my case I will always pass a vector so it's easier to define it like that.