How to make two rows of a matrix correspond to a vector

11 views (last 30 days)
Hi everyone. I've been trying to figure out for the past hour how I can make two columns of a matrix correspond to a vector:
elements =
H
He
Li
Be
B
C
N
O
F
Ne
Na
Mg
Al
Si
P
S
Cl
Ar
atomicweights =
Columns 1 through 7
1.0100 4.0000 6.9400 9.0100 10.8100 12.0100 14.0100
Columns 8 through 14
16.0000 19.0000 20.1800 23.0000 24.3100 26.9800 28.0900
Columns 15 through 18
30.9700 32.0600 35.4500 39.9500
As you can see, the matrix "elements" has two columns, and 18 rows; the vector "atomicweights" has 1 row and 18 elements. I want to make it so " H" corresponds to 1.0100, "He" corresponds to 4.0000, etc..
Thanks in advance!

Answers (2)

Andrei Bobrov
Andrei Bobrov on 4 Oct 2013
Edited: Andrei Bobrov on 4 Oct 2013
s = {'H';'He';'Li';'Be';'B';'C';'N';'O';'F';'Ne';'Na';'Mg';'Al';'Si';'P';'S';'Cl';'Ar'};
n = [1.0100 4.0000 6.9400 9.0100 10.8100 12.0100 14.0100 16.0000 19.0000 20.1800 23.0000 24.3100 26.9800 28.0900 30.9700 32.0600 35.4500 39.9500];
out = [s, num2cell(n)];
or
elements = cell2struct(num2cell(n'),s,1);
> elements.H
ans =
1.0100

Jos (10584)
Jos (10584) on 4 Oct 2013
For linear indexing it does not matter how a vector is stored internally. It matters for display purposes (and computations!).
A = [1 2 3]
B = [10 ; 20 ; 30] % row in B corresponds to column in A
ix = 2 ;
A(ix)
B(ix)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!