How to remove 0 (for 'double' numerical type) or [] (for cell type) rows in a table ?
2 次查看(过去 30 天)
显示 更早的评论
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).
0 个评论
回答(1 个)
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
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
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 Exchange 中查找有关 Deployable Archive Creation 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!