How to add row and column labels to matrix?

57 visualizaciones (últimos 30 días)
chocho
chocho el 24 de Mayo de 2018
Editada: chocho el 24 de Mayo de 2018
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

Respuesta aceptada

Ameer Hamza
Ameer Hamza el 24 de Mayo de 2018
Editada: Ameer Hamza el 24 de Mayo de 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 comentarios
Ameer Hamza
Ameer Hamza el 24 de Mayo de 2018
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(:))];
chocho
chocho el 24 de Mayo de 2018
Editada: chocho el 24 de Mayo de 2018
@Ameer Hamza Thank you too too much, well done!

Iniciar sesión para comentar.

Más respuestas (2)

KSSV
KSSV el 24 de Mayo de 2018
row = {'head1' ; 'head2' ; 'head3' } ;
head1 = rand(3,1) ;
head2 = rand(3,1) ;
head3 = rand(3,1) ;
T = table(row,head1,head2,head3)
  1 comentario
chocho
chocho el 24 de Mayo de 2018
Editada: chocho el 24 de Mayo de 2018
@KSSV I have 135 heads How to generalize this code? and my matrix is of size 135*135 too

Iniciar sesión para comentar.


Akira Agata
Akira Agata el 24 de Mayo de 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 comentarios
Akira Agata
Akira Agata el 24 de Mayo de 2018
Editada: Akira Agata el 24 de Mayo de 2018
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
chocho
chocho el 24 de Mayo de 2018
suffix = arrayfun(@(k) {sprintf('Ver%03d_',k)},(1:N)');
N is not defined ? i have change by k but still have problem
it says:
'Ver064_ptr04550_Signaling_pathways_regulating_pluripotency_of_stem_cells' is not a valid variable name.
I can share the data if you want?

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by