How to remove 0 (for 'double' numerical type) or [] (for cell type) rows in a table ?
조회 수: 6 (최근 30일)
이전 댓글 표시
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/157998/image.png)
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!