Matrix row and column headers

13 次查看(过去 30 天)
Jackson Contreras
Jackson Contreras 2019-4-11
I have created a 15 x 15 arrray called M.
This array should have 15 city names going across the top and then the same 15 city names going across the side. Row & Column headers. I have made the array and row header, I cannot make the column header for some peculiar reason.
M = [0,1...] //my matrix
Rows = {'Portland'; 'St. Louis'; ... } //works fine i get a Rows list that is 15 x 1
Columns = {'Portland', 'St. Louis', ... } // My attempt to make a 1 x 15 column list. Fails everytime.
Can someone help me to make a columns header then add it and my row headers to my Array? I have looked up many questions but I cannot get past making the columns header.

回答(1 个)

Akshat
Akshat 2024-11-5,11:24
To give row and column headers to your matrix in MATLAB, I would strongly recommend using the data structure called "table" in MATLAB.
There is a function called "array2table" in MATLAB, using which you can make a pre existing matrix into a table, with row headers and column headers.
The documentation regarding "array2table" can be found on this link:
A boilerplate code to suit your use case can be seen as follows:
M = randi(100, 15, 15); % Replace with your actual data
Rows = {'Portland'; 'St. Louis'; 'New York'; 'Los Angeles'; 'Chicago';
'Houston'; 'Phoenix'; 'Philadelphia'; 'San Antonio'; 'San Diego';
'Dallas'; 'San Jose'; 'Austin'; 'Jacksonville'; 'Fort Worth'};
Columns = {'Portland', 'St. Louis', 'New York', 'Los Angeles', 'Chicago',
'Houston', 'Phoenix', 'Philadelphia', 'San Antonio', 'San Diego',
'Dallas', 'San Jose', 'Austin', 'Jacksonville', 'Fort Worth'};
T = array2table(M, 'VariableNames', Columns, 'RowNames', Rows);
disp(T);
Portland Houston Dallas St. Louis Phoenix San Jose New York Philadelphia Austin Los Angeles San Antonio Jacksonville Chicago San Diego Fort Worth ________ _______ ______ _________ _______ ________ ________ ____________ ______ ___________ ___________ ____________ _______ _________ __________ Portland 18 92 88 76 15 38 47 67 29 21 100 93 92 47 91 St. Louis 29 37 1 4 63 81 28 5 42 59 24 5 88 48 39 New York 49 33 77 82 92 94 65 38 77 72 69 53 79 24 15 Los Angeles 67 40 19 54 58 59 35 36 72 37 54 90 9 32 98 Chicago 12 1 70 43 5 82 81 56 15 2 62 36 54 90 70 Houston 37 64 43 78 17 79 57 67 59 75 97 31 22 60 20 Phoenix 36 10 84 100 8 99 68 4 63 81 77 8 14 96 10 Philadelphia 5 62 8 14 65 88 68 71 95 71 84 38 61 98 68 San Antonio 89 1 44 12 64 83 59 99 51 22 98 49 71 71 58 San Diego 73 77 23 80 33 25 37 79 70 70 68 5 11 82 50 Dallas 78 7 47 82 93 60 60 93 25 1 53 16 37 57 5 San Jose 22 55 53 5 67 10 75 6 27 9 29 35 64 66 68 Austin 86 85 84 95 43 69 81 100 54 59 12 38 68 38 32 Jacksonville 68 17 46 48 70 72 33 39 24 87 72 7 39 21 40 Fort Worth 27 49 5 8 35 54 29 74 30 9 100 99 14 77 2
Let me know in the comments if you have any further questions!

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by