2011-07-01 40 views
20

Có cách nào thuận tiện để hiển thị ma trận có nhãn hàng và cột trong đầu cuối Matlab không? Một cái gì đó như thế này:Ma trận hiển thị có nhãn hàng và cột

M = rand(5); 
displaymatrix(M, {'FOO','BAR','BAZ','BUZZ','FUZZ'}, ... 
       {'ROW1','ROW2','ROW3','ROW4','ROW5'}); %?? 

năng suất:

 FOO  BAR  BAZ  BUZZ  FUZZ 
ROW1 0.1622 0.4505 0.1067 0.4314 0.8530 
ROW2 0.7943 0.0838 0.9619 0.9106 0.6221 
ROW3 0.3112 0.2290 0.0046 0.1818 0.3510 
ROW4 0.5285 0.9133 0.7749 0.2638 0.5132 
ROW5 0.1656 0.1524 0.8173 0.1455 0.4018 

Thậm chí tốt hơn sẽ là một cái gì đó với một số niceties ASCII-art:

 | FOO  BAR  BAZ  BUZZ  FUZZ 
-----+------------------------------------------------- 
ROW1 | 0.1622 0.4505 0.1067 0.4314 0.8530 
ROW2 | 0.7943 0.0838 0.9619 0.9106 0.6221 
ROW3 | 0.3112 0.2290 0.0046 0.1818 0.3510 
ROW4 | 0.5285 0.9133 0.7749 0.2638 0.5132 
ROW5 | 0.1656 0.1524 0.8173 0.1455 0.4018 
+0

Câu hỏi này là từ năm 2011, câu trả lời được chấp nhận từ năm 2012.Matlab2013b đã giới thiệu kiểu dữ liệu 'table', thực hiện chính xác những gì được hỏi trong câu hỏi ban đầu (xem câu trả lời của Sh3ljohn). –

Trả lời

19

Matlab có chức năng gọi là printmat trong hộp công cụ Hệ thống điều khiển. Nó nằm trong thư mục "ctrlobsolete", vì vậy chúng ta có thể giả định rằng nó được coi là "lỗi thời", nhưng nó vẫn hoạt động.

Sự giúp đỡ văn bản là:

>> help printmat 
printmat Print matrix with labels. 
    printmat(A,NAME,RLAB,CLAB) prints the matrix A with the row labels 
    RLAB and column labels CLAB. NAME is a string used to name the 
    matrix. RLAB and CLAB are string variables that contain the row 
    and column labels delimited by spaces. For example, the string 

     RLAB = 'alpha beta gamma'; 

    defines 'alpha' as the label for the first row, 'beta' for the 
    second row and 'gamma' for the third row. RLAB and CLAB must 
    contain the same number of space delimited labels as there are 
    rows and columns respectively. 

    printmat(A,NAME) prints the matrix A with numerical row and column 
    labels. printmat(A) prints the matrix A without a name. 

    See also: printsys. 

Ví dụ:

>> M = rand(5); 
>> printmat(M, 'My Matrix', 'ROW1 ROW2 ROW3 ROW4 ROW5', 'FOO BAR BAZ BUZZ FUZZ') 

My Matrix = 
         FOO   BAR   BAZ   BUZZ   FUZZ 
     ROW1  0.81472  0.09754  0.15761  0.14189  0.65574 
     ROW2  0.90579  0.27850  0.97059  0.42176  0.03571 
     ROW3  0.12699  0.54688  0.95717  0.91574  0.84913 
     ROW4  0.91338  0.95751  0.48538  0.79221  0.93399 
     ROW5  0.63236  0.96489  0.80028  0.95949  0.67874 
3

Dưới đây là một cách tiếp cận nhanh chóng và dơ bẩn:

horzcat({'';'ROW1';'ROW2';'ROW3';'ROW4';'ROW5'}, ... 
     vertcat({'FOO','BAR','BAZ','BUZZ','FUZZ'},... 
       num2cell(rand(5)))) 

năng suất:

ans = 

    ''  'FOO'  'BAR'  'BAZ'  'BUZZ'  'FUZZ' 
    'ROW1' [0.3015] [0.6665] [0.0326] [0.3689] [0.6448] 
    'ROW2' [0.7011] [0.1781] [0.5612] [0.4607] [0.3763] 
    'ROW3' [0.6663] [0.1280] [0.8819] [0.9816] [0.1909] 
    'ROW4' [0.5391] [0.9991] [0.6692] [0.1564] [0.4283] 
    'ROW5' [0.6981] [0.1711] [0.1904] [0.8555] [0.4820] 
+0

các dấu ngoặc vuông rất mất tập trung. –

13

Có vẻ như dữ liệu của bạn có cấu trúc nào đó để bạn có thể đặt nó trong một lớp có cấu trúc hơn - dataset, một phần của hộp công cụ Thống kê.

>> M = rand(5); 
>> dataset({M 'FOO','BAR','BAZ','BUZZ','FUZZ'}, ... 
       'obsnames', {'ROW1','ROW2','ROW3','ROW4','ROW5'}) 

ans = 
      FOO  BAR   BAZ  BUZZ   FUZZ  
    ROW1 0.52853  0.68921 0.91334  0.078176  0.77491 
    ROW2 0.16565  0.74815 0.15238  0.44268  0.8173 
    ROW3 0.60198  0.45054 0.82582  0.10665  0.86869 
    ROW4 0.26297 0.083821 0.53834  0.9619 0.084436 
    ROW5 0.65408  0.22898 0.99613 0.0046342  0.39978 

Ngoài ra, nếu bạn đang xuất bản đầu ra của bạn, đây là one example của một số chức năng sẽ đưa một ma trận w/hàng, tên col và tạo ra một bảng định dạng html.

+1

Chỉ cần thêm rằng Matlab đã thêm một [bảng] (http://blogs.mathworks.com/loren/2013/09/10/introduction-to-the-new-matlab-data-types-in-r2013b/) kiểu dữ liệu trong năm 2013b tương tự với tập dữ liệu 'nhưng không yêu cầu hộp công cụ thống kê – Dan

0

Tôi đã viết một số mã mà có thể hữu ích - nó sử dụng chức năng 'printmat', nhưng thay đổi các đầu vào để chúng là các vectơ di động có chứa các nhãn &/hoặc hàng. hy vọng nó hữu ích.

function out = dispmat(M,row_labels,col_labels); 
%% Matthew Oberhardt 
% 02/08/2013 
% intended to display a matrix along with row and column labels. 
% % ex: 
% M = rand(2,3); 
% row_labels = {'a';'b'}; 
% col_labels = {'c 1','c2 ','c3'}; 
% % if there are no labels for rows or cols, put '' as the input. 
% row_labels = ''; 

%% check that the row & col labels are the right sizes 
[nrows,ncols] = size(M); 

%% populate if either of the inputs is empty 
if isempty(row_labels) 
    row_labels = cell(1,nrows); 
    for n = 1:nrows 
     row_labels{1,n} = '|'; 
    end 
end 
if isempty(col_labels) 
    col_labels = cell(1,ncols); 
    for n = 1:ncols 
     col_labels{1,n} = '-'; 
    end 
end 

assert(length(row_labels)==nrows,'wrong # of row labels'); 
assert(length(col_labels)==ncols,'wrong # of col labels'); 

row_labels = reshape(row_labels,1,length(row_labels)); 
col_labels = reshape(col_labels,1,length(col_labels)); 

%% remove spaces (since they are separators in printmat.m 
cols = strrep(col_labels, ' ', '_'); 
rows = strrep(row_labels, ' ', '_'); 

%% create labels, space delimited 
c_out = []; 
for n = 1:length(cols) 
    c_out = [c_out,cols{n},' ']; 
end 
c_out = c_out(1:end-1); 

r_out = []; 
for n = 1:length(rows) 
    r_out = [r_out,rows{n},' ']; 
end 
r_out = r_out(1:end-1); 

%% print 
printmat(M, '',r_out,c_out) 
1

Matthew Oberhardt, mã hữu ích, tôi đã thêm tên vào ma trận, đây là nó mã mới rất đơn giản chỉ cần thêm hơn một biến Và tôi cũng cho phép một ví dụ để sử dụng nó, hãy chú ý conv đó là một ma trận mxn.

--------------- MÃ ---------------

function out = dispmat(M,name,row_labels,col_labels); 
%% Matthew Oberhardt 
% 02/08/2013 
% intended to display a matrix along with row and column labels. 
%% ex: 
% M = rand(2,3); 
% row_labels = {'a';'b'}; 
% col_labels = {'c 1','c2 ','c3'}; 
% % if there are no labels for rows or cols, put '' as the input. 
% row_labels = ''; 

%Modified 14.07.2014 
%Nestor Cantu 
%Added the name of the matrix. 

%% check that the row & col labels are the right sizes 
[nrows,ncols] = size(M); 

%% populate if either of the inputs is empty 
if isempty(row_labels) 
row_labels = cell(1,nrows); 
    for n = 1:nrows 
     row_labels{1,n} = '|'; 
    end 
end 
if isempty(col_labels) 
    col_labels = cell(1,ncols); 
    for n = 1:ncols 
     col_labels{1,n} = '-'; 
    end 
end 

assert(length(row_labels)==nrows,'wrong # of row labels'); 
assert(length(col_labels)==ncols,'wrong # of col labels'); 

row_labels = reshape(row_labels,1,length(row_labels)); 
col_labels = reshape(col_labels,1,length(col_labels)); 

%% remove spaces (since they are separators in printmat.m 
cols = strrep(col_labels, ' ', '_'); 
rows = strrep(row_labels, ' ', '_'); 

%% create labels, space delimited 
c_out = []; 
for n = 1:length(cols) 
    c_out = [c_out,cols{n},' ']; 
end 
c_out = c_out(1:end-1); 
r_out = []; 
for n = 1:length(rows) 
    r_out = [r_out,rows{n},' ']; 
end 
r_out = r_out(1:end-1); 

%% print 

printmat(M,name,r_out,c_out) 


end 

-------- --EXAMPLE với ma trận chuyển đổi (5,4) --------------

[m n] = size(conv); 
for i=1:n 
    col{i} = ['K = ' num2str(i)]; 
end 

for i=1:m 
    row{i} = ['n =' num2str(i)]; 
end 

outMat(conv,'Convergence',row',col); 

-------------- RESULT --- -----------------------

Convergence = 
       K_=_1  K_=_2  K_=_3  K_=_4  K_=_5 
    n_=1  0.74218  0.42070  0.11101 9.86259e-006 9.86259e-006 
    n_=2  0.49672  0.26686  0.00233 4.46114e-011 4.46114e-011 
    n_=3  0.01221  0.00488 1.23422e-007   0   0 
    n_=4  0.00010 7.06889e-008 7.06889e-008   0   0 
9

Tôi biết đây là một bài đăng cũ, nhưng tôi tin rằng giải pháp là sử dụng array2table. Cụ thể trong trường hợp của OP, người ta chỉ cần làm:

>> M = rand(5); 
>> names= {'A','B','C','D','E'}; 
>> array2table(M, 'VariableNames', names, 'RowNames', names) 

ans = 

      A   B   C   D   E  
     _______ _______ _______ _______ ________ 

    A 0.81472 0.09754 0.15761 0.14189  0.65574 
    B 0.90579  0.2785 0.97059 0.42176 0.035712 
    C 0.12699 0.54688 0.95717 0.91574  0.84913 
    D 0.91338 0.95751 0.48538 0.79221  0.93399 
    E 0.63236 0.96489 0.80028 0.95949  0.67874 
Các vấn đề liên quan