1

I have a 15x7 matrix of doubles called A . What I want to do it to add one extra row and column as a title name for the rows and column which will be saved as matrix B which will be of size 16x8.

A=

1   1   -27.84543037    235.8829193 1529.838379 309.5192566 151.2160492
1   1   -25.76914215    22.570755   1521.290161 310.2462463 231.4568634
1   1   -162.0449066    19.34294128 1515.019287 258.4113464 232.6483002
1   1   -316.8930359    -106.8941193    1407.015625 190.3127441 283.7460022
1   1   -300.2756958    139.7964783 1365.73584  193.3990479 181.0596008
1   1   110.5066299 25.79856873 1527.561035 361.6555786 230.2752075
1   1   296.7176208 -125.0899811    1428.061768 439.6409607 290.4381409
1   1   281.2500305 118.227478  1377.988037 437.5250854 190.5966339
1   1   -21.2894783 -170.6610107    1523.40271  311.9530029 304.5064392
1   1   -105.9274673    -366.0036011    1521.414429 279.9092102 378.5228271
1   1   -178.0385742    -654.3458252    1275.617554 239.6331177 535.3726807
1   1   -247.3761902    -931.5979614    1039.274658 182.9398804 756.1568604
1   1   72.30783844 -361.7819214    1529.616089 347.2199097 376.1908569
1   0.5 137.2854919 -687.1557617    1330.945068 379.3947449 537.2888184
1   0.5 206.1542358 -984.8857422    1119.07019  426.0763245 746.7713623


B=
    AA  BB  CC  DD  EE  FF  GG
EE  1   1   -27.84543037    235.8829193 1529.838379 309.5192566 151.2160492
FF  1   1   -25.76914215    22.570755   1521.290161 310.2462463 231.4568634
GG  1   1   -162.0449066    19.34294128 1515.019287 258.4113464 232.6483002
HH  1   1   -316.8930359    -106.8941193    1407.015625 190.3127441 283.7460022
II  1   1   -300.2756958    139.7964783 1365.73584  193.3990479 181.0596008
JJ  1   1   110.5066299 25.79856873 1527.561035 361.6555786 230.2752075
KK  1   1   296.7176208 -125.0899811    1428.061768 439.6409607 290.4381409
LL  1   1   281.2500305 118.227478  1377.988037 437.5250854 190.5966339
MM  1   1   -21.2894783 -170.6610107    1523.40271  311.9530029 304.5064392
NN  1   1   -105.9274673    -366.0036011    1521.414429 279.9092102 378.5228271
OO  1   1   -178.0385742    -654.3458252    1275.617554 239.6331177 535.3726807
PP  1   1   -247.3761902    -931.5979614    1039.274658 182.9398804 756.1568604
QQ  1   1   72.30783844 -361.7819214    1529.616089 347.2199097 376.1908569
RR  1   0.5 137.2854919 -687.1557617    1330.945068 379.3947449 537.2888184
SS  1   0.5 206.1542358 -984.8857422    1119.07019  426.0763245 746.7713623

I've tried saving the new column in a vector as newcol=['AA','BB','CC,'DD','EE','FF','GG'] and then add it to A like B=[newcol;A]; and then do the same with rows. But it is not working, so if anyone could please advise how can I do this.

Tak
  • 3,536
  • 11
  • 51
  • 93
  • Matrices of doubles can only contain doubles. `AA` is a string, so it can't go in a matrix of numeric data. Why do you want to do this, what do you need to use `B` for? – David May 16 '14 at 04:20
  • Thanks for your comment! I want to be easier to analyze the data instead of checking every time what each cell represents – Tak May 16 '14 at 04:23
  • It might be easier to just add some comments to your code about what `B` looks like. You could also make the columns of `B` into vectors with their own names, eg: `AA=B(:,1)`. It is possible to relate labels to data like this, but I haven't come across a nice way of doing it. Hopefully someone can help us both! – David May 16 '14 at 04:32
  • @user1460166 Look into [this question](http://stackoverflow.com/q/23668450/3293881). OP there is using a structure to handle such cases. Your issue can be resolved if you follow his ways. – Divakar May 16 '14 at 04:38

1 Answers1

3

What you want is table. You can find references here and here.

B = table( A(:,1),A(:,2),A(:,3),A(:,4),A(:,5),A(:,6),A(:,7),...
    'VariableNames', {'AA'; 'BB'; 'CC'; 'DD'; 'EE'; 'FF'; 'GG'},... 
    'RowNames', {'EE','FF','GG','HH','II','JJ','KK','LL','MM','NN','OO','PP','QQ','RR','SS'});

% they will yield the same values
B{2,3}
B{'FF', 'CC'}

Note: it only works for MATLAB R2014A

ysakamoto
  • 2,512
  • 1
  • 16
  • 22