How to insert a new row on top of the matrix with a different label and insert a new column at the end with the same label

9 次查看(过去 30 天)
Hello everyone,
I need to create a new row on top of the 132x500 matrix that labeled with C1:C500 and then, a new column at the end that store the same value which is 'Type A' for the whole 132 data. I did use sym function however it gives a lot of burden to my Matlab until it stops multiple time. Thanks in advanced.
sample = rand(132,500);
labelRow = sym('c', [1 500]);
ResultLabelRow= [labelRow;sample];

采纳的回答

Walter Roberson
Walter Roberson 2017-8-10
Numeric arrays and sym() do not support labels.
The labels you want happen to be acceptable as variable names for a table() object, and they happen to be sequentially numbered in a way that happens to work out, so you could do
C = sample; %magic variable name to make the table work columns work out
ResultRowLabel = array2table(C);
If C does not happen to be available as a variable name to store the data in temporarily, then for R2016b or later,
labelRow = cellstr( string('C') + (1:size(sample,2)) );
ResultRowLabel = array2table(sample, 'VariableNames', labelRow);
For earlier versions,
labelRow = cellstr( num2str((1:size(sample,2)).', 'C%d') );
If you do not wish to use a table, then you will need to go into cell arrays,
labelRow = cellstr( string('C') + (1:size(sample,2)) ) .';
ResultRowLabel = [labelRow; num2cell(sample)];

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by