How do you label the rows and columns of an array?

30 ビュー (過去 30 日間)
Fred
Fred 2012 年 2 月 10 日
I want to label each column and row of the array, so I don't get confused or forget what each set of numbers represents.
So I have an array, M:
M = [1 2 3; 3 4 5; 6 7 8];
%Create string of titles:
rowtitle = {'row1','row2','row3'};
columntitle = {'col1','col2','col3'};
So now, how do I add these titles to the array?
  2 件のコメント
Sean de Wolski
Sean de Wolski 2012 年 2 月 10 日
May I ask why you want this information? It could be generated at any time by knowing the location.
Image Analyst
Image Analyst 2012 年 2 月 10 日
Is this perhaps because you want to stuff everything into an Excel workbook?

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

回答 (3 件)

the cyclist
the cyclist 2012 年 2 月 10 日
If you want to mix text and numeric values in an array, you could use a cell array.
>> help cell
will give a tiny bit of syntax. There is abundant documentation on using cell arrays in MATLAB.

Andrei Bobrov
Andrei Bobrov 2012 年 2 月 10 日
[rows,columns,Mvalues] = find(M)

Matt Tearle
Matt Tearle 2012 年 2 月 10 日
MATLAB doesn't have a way to tag meta data onto an array. There are, however, several ways to do something like you've asked, but the best option depends heavily on what you're trying to do with the data.
Some options I can think of:
  • Use a cell array: x = [{''},columntitle;rowtitle',num2cell(M)]
  • Just keep the three variables and index into them. E.g., which column contains the maximum value of M? [~,k] = max(max(M)); columntitle{k}
  • Use a dataset with columntitle as the VarNames and rowtitle as the ObsNames (requires Statistics Toolbox)
  • Use structures

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by