Put name on above and left
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Seongyun Kim
le 1 Mar 2022
Commenté : Davide Masiello
le 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 commentaires
Réponse acceptée
Davide Masiello
le 1 Mar 2022
Modifié(e) : Davide Masiello
le 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 commentaires
Plus de réponses (1)
Les Beckham
le 1 Mar 2022
Modifié(e) : Les Beckham
le 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 commentaires
Voir également
Catégories
En savoir plus sur Tables dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!