Add row and column headers to 3d matrix
Show older comments
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 Comments
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
on 13 Dec 2018
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
on 13 Dec 2018
Jan
on 13 Dec 2018
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
on 13 Dec 2018
+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
on 13 Dec 2018
"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:
Accepted Answer
More Answers (1)
Omer Yasin Birey
on 13 Dec 2018
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;
Categories
Find more on Matrix Indexing 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!