How to remove 0 (for 'double' numerical type) or [] (for cell type) rows in a table ?

1 回表示 (過去 30 日間)
Pierre Lonfat
Pierre Lonfat 2016 年 11 月 13 日
コメント済み: Walter Roberson 2016 年 11 月 14 日
Sorry I am new to matlab ! Any help would be really appreciated ! Have a nice evening !
Ps: I tried the function unique but and as usual on matlab, it doesn't work. I guess its because there are two types in the table ('cell' for bankname and 'double' for the other categories).

回答 (1 件)

Giovanni Mottola
Giovanni Mottola 2016 年 11 月 13 日
Starting point:
tab =
S_Bankname S_CommonEquityTierRatio_2013 S_Log_TotalAssets_2013
__________ ____________________________ ______________________
'' 0.00000000000000e+00 0.00000000000000e+00
'Axa' 1.14661000000000e+00 1.08520000000000e+01
'Carige' 3.90410000000000e-02 1.07440000000000e+01
'MPS' 6.98750000000000e-02 1.23110000000000e+01
'' 0.00000000000000e+00 0.00000000000000e+00
'Cyprus' 7.28620000000000e-02 1.03870000000000e+01
Assuming that the rows to delete have 0 in the second column:
toDelete = tab.S_CommonEquityTierRatio_2013==0;
tab(toDelete,:) = [];
Final result:
tab =
S_Bankname S_CommonEquityTierRatio_2013 S_Log_TotalAssets_2013
__________ ____________________________ ______________________
'Axa' 1.14661000000000e+00 1.08520000000000e+01
'Carige' 3.90410000000000e-02 1.07440000000000e+01
'MPS' 6.98750000000000e-02 1.23110000000000e+01
'Cyprus' 7.28620000000000e-02 1.03870000000000e+01
  4 件のコメント
Giovanni Mottola
Giovanni Mottola 2016 年 11 月 14 日
編集済み: Giovanni Mottola 2016 年 11 月 14 日
I just tried the code you wrote with the initial data as follows:
S_Bankname={'', 'Axa', 'Carige', 'MPS', '', 'Cyprus'}.';
S_CommonEquityTier1Ratio_2013=[0, 1.14661, 0.039041, 0.069875, 0, 0.072862].';
S_Log_TotalAssets_2013=[0, 10.852, 10.744, 12.311, 0, 10.387].';
I used only a subset of your table to keep things simple.
Using the commands you wrote, that is,
S_Test_Table=table(S_Bankname,S_CommonEquityTier1Ratio_2013,S_Log_TotalAssets_2013)
toDelete = S_Test_Table.S_CommonEquityTier1Ratio_2013==0;
S_Test_Table(toDelete,:) = [];
the table does change and the empty lines are deleted.
Could it be that, in your table, the "empty" rows don't actually have zeros in the second column but instead very small values? Could you provide the full data for your table?
Walter Roberson
Walter Roberson 2016 年 11 月 14 日
Deleting a row in a table works fine.
a = randi(6,3,1); b = {'hello';'';'zumba'};
t = table(a,b);
toDelete = cellfun(@isempty,t.b);
t(toDelete,:) = [];

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeTables についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by