2
$\begingroup$

I have data file with the following structure:

{{100, 201, value1}, {100, 301, value2}, {200, 301, value3}}

How to construct a function f[i_,j_] so that f[100,201]=value1, f[100,301]=value2, f[200,301]=value3. For all other arguments which are not presented in a file one have to obtain zero, i.e. f[100,104]=0?

P.S. This problem arises in connection with atomic physics. It is necessary to construct a function which calls the transitions rates between states i and j tabulated in a file. i=100 means 1S state of hydrogen for example, j=201 = 2p state and etc.

$\endgroup$

2 Answers 2

1
$\begingroup$
f[_, _]=0;

(f[#, #2] = #3) & @@@ {{100, 201, value1}, {100, 301, value2}, {200, 301, value3}};


f[100,201]
f[100,104]
$\endgroup$
1
  • $\begingroup$ Great!!! Thank you! $\endgroup$ Commented Mar 12, 2018 at 12:49
1
$\begingroup$

This is essentially Lookup with a default.

data = {{100, 201, value1}, {100, 301, value2}, {200, 301, value3}}
listOfRules = Most[#] -> Last[#] & /@ data
f = Function[{x, y}, Lookup[listOfRules, Key@{x, y}, 0]]
$\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.