Put name on above and left
3 views (last 30 days)
Show older comments
Seongyun Kim
on 1 Mar 2022
Commented: Davide Masiello
on 1 Mar 2022
How can I put the name on above and left side of the matrix?
For example, I have a matrix but there are only numbers (In fact I have more than 100 numbers)
1 5 4 8
1 2 4 8
5 8 9 2
2 6 3 4
If I want to put A, B, C, D, and E on the head and X, Y, Z and N on the left, how can I do that through the code?
It should be:
A B C D E
X 1 5 4 8
Y 1 2 4 8
Z 5 8 9 2
N 2 6 3 4
0 Comments
Accepted Answer
Davide Masiello
on 1 Mar 2022
Edited: Davide Masiello
on 1 Mar 2022
All elements in an array must be of the same type, i.e. you cannot merge numbers with characters unless you turn the numbers into characters too.
You could do this
A = rand(4,5);
header = ["B" "C" "D" "E" "F"];
fields = ["A" "X" "Y" "Z" "N"];
A = [fields',[header;A]]
2 Comments
More Answers (1)
Les Beckham
on 1 Mar 2022
Edited: Les Beckham
on 1 Mar 2022
This is close to what you can do with a table. However, since you only have four columns in your data array, I don't understand why you want labels A through E across the top as column labels (or what is referred to as the VariableNames of a table).
See if this is what you actually want:
A = [1 5 4 8
1 2 4 8
5 8 9 2
2 6 3 4];
array2table(A, 'RowNames', {'X', 'Y', 'Z', 'N'}, 'VariableNames', {'A', 'B', 'C', 'D'})
0 Comments
See Also
Categories
Find more on Tables 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!