How to add row and column labels to matrix?
46 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Ameer Hamza
2018 年 5 月 24 日
編集済み: Ameer Hamza
2018 年 5 月 24 日
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 件のコメント
Ameer Hamza
2018 年 5 月 24 日
In that case, you don't need to make the table first according to the code in the answer. You can make it like this
x = rand(4);
x = x';
label = compose('head%d', 1:4)';
labelColumnsIndex = fliplr( combvec((1:length(label)), (1:length(label)))' );
labelColumns = label(labelColumnsIndex);
T = [array2table(labelColumns) array2table(x(:))];
その他の回答 (2 件)
KSSV
2018 年 5 月 24 日
row = {'head1' ; 'head2' ; 'head3' } ;
head1 = rand(3,1) ;
head2 = rand(3,1) ;
head3 = rand(3,1) ;
T = table(row,head1,head2,head3)
Akira Agata
2018 年 5 月 24 日
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 件のコメント
Akira Agata
2018 年 5 月 24 日
編集済み: Akira Agata
2018 年 5 月 24 日
Maybe you need not only to remove duplication, but also to remove/replace special characters from row name. Following would be one possible solution.
% Sample data (label contains duplication and special characters)
x = rand(4);
label = {...
'head1';...
'head2';...
'ptr04015:Rap1 signaling pathway';...
'ptr04015:Rap1 signaling pathway'};
% Replace special character to '_'
label = regexprep(label,'\W','_');
% To avoid duplication
suffix = arrayfun(@(k) {sprintf('Ver%03d_',k)},(1:N)');
label = strcat(suffix,label);
T = array2table(x,'RowNames',label,'VariableNames',label);
The output is:
>> T
T =
4×4 table
Ver001_head1 Ver002_head2 Ver003_ptr04015_Rap1_signaling_pathway Ver004_ptr04015_Rap1_signaling_pathway
____________ ____________ ______________________________________ ______________________________________
Ver001_head1 0.8909 0.14929 0.81428 0.1966
Ver002_head2 0.95929 0.25751 0.24352 0.25108
Ver003_ptr04015_Rap1_signaling_pathway 0.54722 0.84072 0.92926 0.61604
Ver004_ptr04015_Rap1_signaling_pathway 0.13862 0.25428 0.34998 0.47329
参考
カテゴリ
Help Center および File Exchange で Axis Labels についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!