1
$\begingroup$

To begin with, I'm a poor English speaker, which means it would be hard to read to natives... I was trying to memorize mRNA codons with mathematica.

mRNA={"U","C","A","G"}
Flatten[Table[f[{X,Y,Z}],{X,mRNA},{Y,mRNA},{Z,mRNA}],2]

A code above is what I wrote and what I want to do is to assign a number. Ex) f[{"U","U","U"}]=1, f[{"U","U","C"}]=2 ,etc/

So I tried a code below

Flatten[Table[f[{X,Y,Z}],{X,mRNA},{Y,mRNA},{Z,mRNA}],2]=Range[64]

But it causes a Set::Write error. How can I fix this?

$\endgroup$

2 Answers 2

1
$\begingroup$

Something like

MapIndexed[(f[#1]=#2[[1]])&, Tuples[mRNA, 3]]

On the other hand, an Association may be better to use:

A = AssociationThread[Tuples[mRNA, 3] -> Range[64]]

which you can then use with

A[{"U","U","C"}]

2

$\endgroup$
1
  • $\begingroup$ Thanks! What a nice method! $\endgroup$
    – 김기훈
    Apr 27, 2019 at 1:25
1
$\begingroup$

Wrap Evaluate around the LHS of the assignment:

Evaluate[Flatten[Table[f[{X, Y, Z}], {X, mRNA}, {Y, mRNA}, {Z, mRNA}], 2]] = Range[64];
$\endgroup$
1
  • $\begingroup$ Thank you! You saved my time. $\endgroup$
    – 김기훈
    Apr 27, 2019 at 1:27

Your Answer

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

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