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

10 views (last 30 days)
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];

Accepted Answer

Walter Roberson
Walter Roberson on 10 Aug 2017
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)];

More Answers (0)

Categories

Find more on Characters and Strings in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!