Matrix row and column headers
조회 수: 13 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
답변 (1개)
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);
Let me know in the comments if you have any further questions!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!