2016-01-12 58 views
7

我如何翻轉檯,使得VariableNames成爲RowNames如何轉置一個MATLAB表格?

m0   m1   m10  m11  m12  m13  m14  m2   m3   m4   m5   m6   m7   m8   m9 
    ________ _______ _______ _______ _______ _______ _______ _______ _______ _______ ________ ________ _______ _______ _______ 

    0.096898 0.11567 0.23266 0.11393 0.51438 0.51438 0.51438 0.42039 0.11543 0.11024 0.060229 0.086558 0.11542 0.11537 0.43305 

成爲

 Chisq 
     _______ 
m0  0.096898 
m1  0.11567 
m2  ... 
...  ... 

回答

9

你需要旋轉和其轉換回之前,你的表先轉換成數組表:

YourArray = table2array(YourTable); 
YourNewTable = array2table(YourArray.'); 
YourNewTable.Properties.RowNames = YourTable.Properties.VariableNames; 

您也可以嘗試rot90(YourTable)看看會發生什麼,但我不知道它是相同的(我認爲這是那些誤導性名字之一)

+0

這可以縮短到'newtable的表=(YourTable {:,:}「,... 'RowNames',YourTable.Properties.VariableNames)'。 'rot90'不接受表格。 – EBH

+1

如果你的表有該行的名稱,添加這一行:'YourNewTable.Properties.VariableNames = YourTable.Properties.RowNames;' – traindriver

+0

我也做了相同的。我的變量是不同的數據類型(uint8,float32等)。當我創建新表時,所有變量都被轉換爲uint16。我怎樣才能避免這種情況? – JohnDoe

0
function [tableTransposed] = transposeTable(tableIn) 
%this function transposes a table. 
props =tableIn.Properties.VariableNames; 

tableTransposed = table(); 
tableSz = size(tableIn); 
tableTransposed.metricName = props'; 
tableTransposed(1,:) = []; 
for newPropertyNum = 1:tableSz(1) 
    propCurr = table2array(tableIn(newPropertyNum,1)); 
    if isa(propCurr,'numeric') 
     newProperty = num2str(propCurr); 
    else %assumed to be string 
     newProperty = propCurr; 

    end 
    tableTransposed = setfield(tableTransposed,newProperty,table2array(tableIn(newPropertyNum,2:end))'); 
end 
+0

這與接受的答案有何不同? – excaza

相關問題