フィルターのクリア

Add row and column headers to 3d matrix

5 ビュー (過去 30 日間)
Kevin Teh
Kevin Teh 2018 年 12 月 13 日
コメント済み: Adam Danz 2018 年 12 月 14 日
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 件のコメント
Kevin Teh
Kevin Teh 2018 年 12 月 13 日
Hi
The goal was to get something like this is my other question:
I'm trying to create an index as such.
Kevin
Stephen23
Stephen23 2018 年 12 月 13 日
編集済み: Stephen23 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:

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2018 年 12 月 13 日
編集済み: Adam Danz 2018 年 12 月 13 日
This example creates row and column headers and puts your data and the headers into a cell array.
% Create fake data
Z = rand(164, 167, 59);
% Generate headers
rowhead = strsplit(sprintf('row%d ', 1:size(Z,1)))'; %Column vector (ie, {'row1', 'row2', 'row3', ...})
rowhead(end) = [];
colhead = strsplit(sprintf('col%d ', 1:size(Z,2))); %Row vector (ie, {'col1', 'col2', 'col3', ...})
colhead(end) = [];
% Put matrix in a cell array, add headers. The upper left corner (Zc(1,1) will be empty)
Zc = cell(size(Z) + [1 1 0]); %Create emtpy cell
Zc(2:end, 1, :) = repmat(rowhead, 1, size(Z,3)); %Place row headers in left "wall" of the 3D array.
Zc(1, 2:end, :) = repmat(colhead, size(Z,3), 1)'; %Place col headers in upper "ceiling" of 3D array.
Zc(2:end, 2:end, :) = num2cell(Z); %Fill the cell array with your data (time killer!)
% Test it
Zc(1,5) %this should be col 4
Zc(21,1) % this should be row 20
  3 件のコメント
Kevin Teh
Kevin Teh 2018 年 12 月 14 日
Thank you.
Adam Danz
Adam Danz 2018 年 12 月 14 日
Thanks, Jan. I didn't know about the undocumented sprintfc() function.

サインインしてコメントする。

その他の回答 (1 件)

Omer Yasin Birey
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;

カテゴリ

Help Center および File ExchangeCharacters and Strings についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by