Filter löschen
Filter löschen

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

11 Ansichten (letzte 30 Tage)
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];

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 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)];

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by