2016-06-20 16 views
0

在Matlab中,我有2個表,其中1個表包含所有其他表的值。第一臺被命名爲T1如何根據Matlab中的RowNames計算表中的值

freq = [2;3;4;5;6;54;3;4]; 
words = {'finn';'jake';'iceking';'marceline';'shelby';'bmo';'naptr';'simon'}; 

T1 = table(freq,... 
     'RowNames',words) 

表2是

freq = [10;3;6;3] 
words = {'finn';'jake';'simon';'shelby'} 
T2 = table(freq,... 
     'RowNames',words) 

如何使用值從T2到T1和打印這樣的:

T3= 
                        freq2 
finn  %is scanned from T2, words that arent contain in T2, is ignored  2/10 %(2 is taken from T2)  
jake                   3/3 %(3 is taken from T2) 
iceking                  4 or 0 or etc %(as long as this name is ignored) 
marceline                  5 or 0 or etc %(as long as this name is ignored) 
shelby                  6/3 %(as long as this name is ignored) 
bmo                   54 or 0 or etc %(as long as this name is ignored) 
naptr                   3 or 0 or etc %(as long as this name is ignored) 
simon                   4/6 %(6 is taken from T2) 

回答

0

這應該這樣做。

%copy T1 to be T3 
T3=T1; 
%find where the elements in each table are 
[~,T1Ind,T2Ind] = intersect(T1.Properties.RowNames,T2.Properties.RowNames); 

%modify the values 
T3{T1Ind,1}=T3{T1Ind,1}./T2{T2Ind,1}; 

%modify the others if you want to 
T3{~ismember(T1.Properties.RowNames,T2.Properties.RowNames),1}=0; %or etc 

(只是保持preedited如果你需要它周圍的方式)

%If it should be based on the smaller table 
T4 = table(T2.freq./T1{T2.Properties.RowNames,1},'RowNames',T2.Properties.RowNames) 

,如果你要與表中的MATLAB更多的工作,你應該「在表中訪問數據」讀文檔,它真的很適合學習從表中提取數據的不同方法

+1

是的請解釋 –

+0

先生,我還有問題要問。我害怕,我錯誤地問了這個問題。你能幫我嗎? –

+0

例如:我想從T1到T2取RowNames,但是當我這樣做時,會出現一條消息「無法識別的行名'marceline'/'iceking'/'bmo'/'naptr'」,因爲這些名稱不包含在T2中。我想要的是; T3的RowNames來自T1,但T2中沒有的名稱將被忽略。比如,IceKing的freq仍然是4.或者0,或者其他,只要不包含在T2中的那些RowNames被忽略。如何解決這個問題? –

相關問題