フィルターのクリア

Creating a three column table from matrix

12 ビュー (過去 30 日間)
Colby
Colby 2015 年 1 月 5 日
コメント済み: Image Analyst 2015 年 1 月 5 日
If I have a matrix, how can I make a table with three columns that are row number, column number, and then the value for that row-column? And then, add a 4th column with the row-column number? example in picture

採用された回答

Julian Hapke
Julian Hapke 2015 年 1 月 5 日
Hi,
because you want a string in your 4th column, you need a cell array. here's my q&d solution:
A=rand(3,3);
new = cell(numel(A),4);
kk=1;
for ii = 1:size(A,1);
for jj = 1:size(A,2);
new{kk,1}=ii;
new{kk,2}=jj;
new{kk,3}=A(ii,jj);
new{kk,4}=[num2str(ii) '-' num2str(jj)];
kk=kk+1;
end;
end
Regards
Julian
  2 件のコメント
Colby
Colby 2015 年 1 月 5 日
Thank you very much for your help!!!
Image Analyst
Image Analyst 2015 年 1 月 5 日
Note: this solution does not give a table like you asked for, and like the two other solutions give. Tables are a useful new data type introduced in release R2013b.

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

その他の回答 (2 件)

Image Analyst
Image Analyst 2015 年 1 月 5 日
Try this:
row = [1;1;1;2;2;2;3;3;3]
column = repmat([1:3]', [3, 1])
value = [1;2;3;6;7;8;11;12;13]
table1 = table(row, column, value)
% Now make a second table with an additional column of strings.
for k = 1 : length(row)
rMinusc{k} = sprintf('%d - %d', row(k), column(k));
end
rMinusc = rMinusc'; % Transpose
table2 = table(row, column, value, rMinusc)
In the command window:
table2 =
row column value rMinusc
___ ______ _____ _______
1 1 1 '1 - 1'
1 2 2 '1 - 2'
1 3 3 '1 - 3'
2 1 6 '2 - 1'
2 2 7 '2 - 2'
2 3 8 '2 - 3'
3 1 11 '3 - 1'
3 2 12 '3 - 2'
3 3 13 '3 - 3'
  1 件のコメント
Colby
Colby 2015 年 1 月 5 日
Thanks a lot for your help!!!

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


Jorge
Jorge 2015 年 1 月 5 日
編集済み: Jorge 2015 年 1 月 5 日
Should be something like this. Didn't run it, though... The table you seek is in columns. Use table() to arrange.
[n,m]=size(matrix);
counter=0;
for i=1:n
for j=1:m
counter=counter+1;
rows(counter)=i;
columns(counter)=j;
values(counter)=matrix(i,j);
rc{counter}=strcat(i,'-',j)
end
end
table=[rows columns values rc];
  1 件のコメント
Colby
Colby 2015 年 1 月 5 日
Thanks a lot for the help!!!

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

カテゴリ

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