Add row and column headers to 3d matrix
이전 댓글 표시
Hi
I'm trying to find a simple solution to add column and row headers. So I have:
Z(164,167,59) -3d matrix
names(1x164)-row header
names2(1x167) -column header
I tested to see if i can do a 2d and was going to add a loop using a horzcat as such:
ZHDR=[names2;Z(:,:,1)]
Thank you for any help.
Kevin
댓글 수: 9
A matrix contains numerical data but headers usually contain strings. Are your headers numerical too? Or is all of this stored in a cell array rather than a matrix?
If you original data have a size of 164 x 167 x 59, do you expect your header-added data to have the size 165 x 168 x 59?
Jan
2018년 12월 13일
The question is still not clear. Where do you want the header strings to appear? You cannot concatenate strings or char vectors with numerical data, except if the array is stored as a cell. So please explain exactly, what your inputs are and what you want to achieve.
Kevin Teh
2018년 12월 13일
Jan
2018년 12월 13일
I did not meant, if the header appear on the top and on the left, but if you want to write them in a file (text, XLS, CSV?), in the command window, if they should appear in a uitable or whatever.
Adam Danz
2018년 12월 13일
+1 Stephen.
Kevin, you can create the row and column headers and store them independently from your numeric data. Converting to a cell array will slow down your code and you'll have to +1 for each row and column index to account for the headers. Many functions will require you to convert back to matrix format, too. Avoid this method if you can.
Kevin Teh
2018년 12월 13일
"I'm trying to create an index as such"
All numeric arrays already have perfectly usfeul, simple, extremely efficient indices.
You explanation does not make it clear why you need to reinvent the wheel using something complex like you are trying to do, when there already exists something simpler and much more efficient (standard MATLAB indexing).
As Adam Danz already wrote, just keep your header, row, and numeric arrays separate, it will be much more efficient to work with. Or use a table. Or tell us what you are actually trying to achieve:
채택된 답변
추가 답변 (1개)
Omer Yasin Birey
2018년 12월 13일
Hi Kevin;
You may try this:
Z = zeros(165,168,59);
rowheader = cellstr('rowheader');
columnHeader = cellstr('columnHeader');
Z = num2cell(Z);
Z(2:end,1,:) = rowheader;
Z(1,2:end,:) = columnHeader;
카테고리
도움말 센터 및 File Exchange에서 Matrices and Arrays에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!