How to Merge the cells and name?

17 views (last 30 days)
C PRASAD
C PRASAD on 6 Jul 2022
Commented: C PRASAD on 6 Jul 2022
I have the data and I would like to name and merge.The above table the data i have, the below table the table i required.
  1 Comment
Walter Roberson
Walter Roberson on 6 Jul 2022
How does this differ from the previous similar questions you have posted on the topic?

Sign in to comment.

Accepted Answer

Chunru
Chunru on 6 Jul 2022
datayouhave = rand(8, 5); % replace this with your data
t = array2table(datayouhave, "VariableNames", ["Maths", "Physics", "Politics", "Economy", "English"]);
name = ["AAA", "AAA", "AAA", "BBB", "BBB", "BBB", "CCC", "CCC"]';
t = [array2table(name) t];
t
t = 8×6 table
name Maths Physics Politics Economy English _____ _______ _______ ________ ________ _________ "AAA" 0.12481 0.4245 0.89831 0.22978 0.0087086 "AAA" 0.19179 0.89669 0.25311 0.16138 0.49893 "AAA" 0.60817 0.29709 0.65617 0.49247 0.79378 "BBB" 0.18862 0.49918 0.19054 0.047833 0.3502 "BBB" 0.48838 0.55171 0.51338 0.066283 0.39933 "BBB" 0.43513 0.48865 0.21088 0.21244 0.88148 "CCC" 0.25775 0.61049 0.75696 0.80927 0.74499 "CCC" 0.64411 0.27928 0.11384 0.13664 0.14539
  3 Comments
Walter Roberson
Walter Roberson on 6 Jul 2022
Which MATLAB version are you using?
Try
datayouhave = rand(8, 5); % replace this with your data
t = array2table(datayouhave, 'VariableNames', {'Maths', 'Physics', 'Politics', 'Economy', 'English'});
name = {'AAA', 'AAA', 'AAA', 'BBB', 'BBB', 'BBB', 'CCC', 'CCC'}.';
t = [array2table(name) t];
t
t = 8×6 table
name Maths Physics Politics Economy English _______ ________ ________ ________ _________ ________ {'AAA'} 0.057449 0.99295 0.93589 0.88883 0.54624 {'AAA'} 0.20587 0.3941 0.058401 0.93182 0.29469 {'AAA'} 0.6748 0.18482 0.71994 0.48408 0.84695 {'BBB'} 0.51505 0.60256 0.21766 0.99488 0.17571 {'BBB'} 0.32222 0.16554 0.039902 0.64758 0.82738 {'BBB'} 0.95771 0.056249 0.86953 0.94728 0.085409 {'CCC'} 0.82799 0.75418 0.95384 0.0025524 0.52635 {'CCC'} 0.72661 0.97953 0.73226 0.92357 0.97658

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 6 Jul 2022
You cannot create that kind of table in MATLAB.
In order to have a row name span multiple rows, you will need to create a table with three rows and two variables. The first variable will be the Name. For each row, the second variable must be a complete table.
M = round(randn(15, 5), 1)
M = 15×5
-0.2000 -0.5000 -0.1000 -0.9000 1.6000 2.0000 0.5000 0.6000 1.1000 -0.1000 0.4000 1.1000 -0.7000 -1.1000 1.0000 0.1000 0.5000 2.1000 0.8000 -0.4000 0.7000 1.1000 0.5000 -0.4000 1.1000 -0.4000 -0.4000 0.8000 1.5000 -0.7000 -0.7000 -0.2000 0.6000 0.4000 0.6000 0 0.7000 -0.3000 -0.4000 -0.6000 -0.9000 -1.3000 -2.0000 2.4000 -2.2000 -0.4000 -0.6000 -0.7000 1.1000 1.1000
varnames = ["Maths", "Physics", "Politics", "Economy", "English"];
T1 = array2table(M(1:5,:), 'VariableNames', varnames);
T2 = array2table(M(6:10,:), 'VariableNames', varnames);
T3 = array2table(M(11:15,:), 'VariableNames', varnames);
names = ["Ramu"; "Rihith"; "Swarmy"];
Output = table('Size', [3 2], 'VariableTypes', ["string", "table"], 'VariableNames', ["Name"; "Marks"]);
Output.Name = names;
Output.Marks = {T1; T2; T3};
Output
Output = 3×2 table
Name Marks ________ ___________ "Ramu" {5×5 table} "Rihith" {5×5 table} "Swarmy" {5×5 table}
Output.Marks{1}
ans = 5×5 table
Maths Physics Politics Economy English _____ _______ ________ _______ _______ -0.2 -0.5 -0.1 -0.9 1.6 2 0.5 0.6 1.1 -0.1 0.4 1.1 -0.7 -1.1 1 0.1 0.5 2.1 0.8 -0.4 0.7 1.1 0.5 -0.4 1.1
So you can construct the table, but it will not display nicely.

Categories

Find more on Quadratic Programming and Cone Programming 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!