Vectorized atomic masses in MATLAB

It is often convenient to obtain atomic masses for a given array of element names in a vectorized way. For example, one may use it to calculate the weighed RMSD (wRMSD) between two conformations of a protein specified by their atom coordinates \{\, x_i \,\}_{i=1}^n and \{\, y_i \,\}_{i=1}^n:

\mathrm{wRMSD} = \sqrt{\frac{1}{n} \sum_{i=1}^n w_i (x_i - y_i)^2},

where x_i, y_i \in \mathbb{R}^3 and w_i denotes the atomic mass of the ith atom.

Unlike R, MATLAB does not allow to assign custom names for array elements. Instead, it provides a special class containers.Map that represents a dictionary-like data structure. Using this class, we implement a vectorized function that accepts a cell array of element names and returns the corresponding array of atomic masses.


function mass = atomicmass(atomicSymbols)

persistent atomicMasses;

if isempty(atomicMasses)

    symbols = {'H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', ...
        'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', ...
        'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', ...
        'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', ...
        'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', ...
        'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La'};

    massValues = [1 4 6.94 9.01 10.81 12.01 14.01 16 19 20.18 23 24.31 ...
        26.98 28.09 30.97 32.07 35.45 39.95 39.1 40.08 44.96 47.88 ...
        50.94 52 54.94 55.85 58.93 58.69 63.55 65.39 69.72 72.59 74.92 ...
        78.96 79.9 83.8 85.47 87.62 88.91 91.22 92.91 95.94 97.91 ...
        101.07 102.91 106.42 107.87 112.41 114.82 118.71 121.75 127.6 ...
        126.91 131.29 132.91 137.33 138.96];

    atomicMasses = containers.Map(symbols, massValues, ...
        'UniformValues', true);

end

mass = cell2mat(values(atomicMasses, atomicSymbols));

end

Note that we use the persistent keyword to declare atomicMasses in order to keep its value between function calls. During the first function call, the container.Map object is created and assigned to atomicMasses. Also we apply cell2mat in the end of the function to get a regular numeric array of atomic weights instead of a cell array.

Here is the result of calling the function:


atomicmass({'C', 'H', 'O', 'H'})

ans =

   12.0100 1.0000 16.0000 1.0000

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s