如何根据 Matlab 中的行名从表中计算值
How to Calculate Values from Tables, Based On the RowNames in Matlab
在 Matlab 中,我有 2 个 table,1 个 table 包含所有其他 table 的值。第一个 table 被命名为 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 是
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)
这应该可以做到。
%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
(如果您需要它,只需保留预编辑)
%If it should be based on the smaller table
T4 = table(T2.freq./T1{T2.Properties.RowNames,1},'RowNames',T2.Properties.RowNames)
如果您打算更多地使用 tables,您应该阅读 matlab 文档中的 "Access Data in a Table",它对于学习从 table 中提取数据的不同方法非常有用
在 Matlab 中,我有 2 个 table,1 个 table 包含所有其他 table 的值。第一个 table 被命名为 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 是
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)
这应该可以做到。
%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
(如果您需要它,只需保留预编辑)
%If it should be based on the smaller table
T4 = table(T2.freq./T1{T2.Properties.RowNames,1},'RowNames',T2.Properties.RowNames)
如果您打算更多地使用 tables,您应该阅读 matlab 文档中的 "Access Data in a Table",它对于学习从 table 中提取数据的不同方法非常有用