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

13 次查看(过去 30 天)
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!

回答(2 个)

Andrei Bobrov
Andrei Bobrov 2013-10-4
编辑:Andrei Bobrov 2013-10-4
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) 2013-10-4
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)

类别

Help CenterFile Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by