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

조회 수: 6 (최근 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 CenterFile Exchange에서 Deployable Archive Creation에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by