How to add row and column labels to matrix?
98 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello guys, I have a matrix of size 135*135 and a cell array of type strings size 135*1
I want to add this cell array in the row and columns of this matrix.
Example_inputs

Expected_outputs

0 commentaires
Réponse acceptée
Ameer Hamza
le 24 Mai 2018
Modifié(e) : Ameer Hamza
le 24 Mai 2018
Duplicate row names and Variables are not supported in MATLAB, consider adding your labels as an extra column of the table. For example
x = rand(4);
label = compose('head%d', [1 1 2 3])';
cellArray = [[{' '}; label]'; label num2cell(x)];
T = cell2table(cellArray);
T =
5×5 table
cellArray1 cellArray2 cellArray3 cellArray4 cellArray5
__________ __________ __________ __________ __________
' ' 'head1' 'head1' 'head2' 'head3'
'head1' [0.6518] [0.7975] [0.3759] [0.4024]
'head1' [0.1314] [0.7482] [0.8932] [0.6812]
'head2' [0.7244] [0.0342] [0.1829] [0.7504]
'head3' [0.6333] [0.5028] [0.1158] [0.7177]
13 commentaires
Plus de réponses (2)
KSSV
le 24 Mai 2018
row = {'head1' ; 'head2' ; 'head3' } ;
head1 = rand(3,1) ;
head2 = rand(3,1) ;
head3 = rand(3,1) ;
T = table(row,head1,head2,head3)
1 commentaire
Akira Agata
le 24 Mai 2018
OK. Then, how about the following? I believe it will be applicable to 135-by-135.
% Sample data
x = rand(4);
label = {'head1';'head2';'head3';'head4'};
% Store them in table
T = array2table(x,'RowNames',label,'VariableNames',label);
The result looks like:
>> T
T =
4×4 table
head1 head2 head3 head4
_______ ________ _______ ________
head1 0.42176 0.65574 0.67874 0.65548
head2 0.91574 0.035712 0.75774 0.17119
head3 0.79221 0.84913 0.74313 0.70605
head4 0.95949 0.93399 0.39223 0.031833
4 commentaires
Voir également
Catégories
En savoir plus sur Matrices and Arrays dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!