0

In Matlab, I have 2 tables, 1 table contains all of other tables' values. First table is named T1

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

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

Table 2 is

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

How do I use values from T2 into T1 and print like this:

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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

1 Answers1

0

This should do it.

%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

(Just keeping the preedited if you ever need it that way around)

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

if you are going to work more with tables you should read "Access Data in a Table" in the matlab documentation, its really good for learning the different ways to extract data from a table

Finn
  • 2,333
  • 1
  • 10
  • 21
  • sir, i have further question to ask. im afraid, i asked the question wrongly. would you help me? – Ivan Bagaskara Jun 20 '16 at 10:53
  • example: I want to take RowNames from T1 to T2, but when i do that, there's a message "Unrecognized row name 'marceline'/'iceking'/'bmo'/'naptr'" because those names arent contain in T2. What i want is; T3's RowNames are from T1, but the names that arent in T2 is ignored. Like, IceKing's freq is still 4. Or 0, or whatever, as long as those RowNames that arent contain in T2 is ignored. How to solve that? – Ivan Bagaskara Jun 20 '16 at 11:22
  • i will look into that. please adapt and extend your question at the top, so if other people having the same issue they find the right threat – Finn Jun 20 '16 at 11:33
  • YOU ARE THE MAN FINN THE HUMAN! THANK YOU SO MUCH. i have another problem but harder than this. i cant post it right now, so i will post it in 90 minutes – Ivan Bagaskara Jun 20 '16 at 12:37
  • Finn, i have another question [Click Here](http://stackoverflow.com/questions/37924343/how-to-scan-values-from-each-words-based-on-tables-and-then-calculate-it) – Ivan Bagaskara Jun 20 '16 at 13:54