A compact way to remove empty rows in a cell array matrix ?

A compact way to remove empty rows in a cell array matrix, as the following one?
>> a
a =
8×2 cell array
{'22-Jul-2021 17:00:00'} {[ 49]}
{0×0 double } {0×0 double}
{0×0 double } {0×0 double}
{0×0 double } {0×0 double}
{0×0 double } {0×0 double}
{'27-Jul-2021 20:00:00'} {[ 123]}
{'28-Jul-2021 06:00:00'} {[ 21]}
{'29-Jul-2021 13:00:00'} {[ 66]}
I tried this solution, but is there any more compact way ?
i = cellfun(@isempty,a);
a( any(i,2), : ) = []
a =
4×2 cell array
{'22-Jul-2021 17:00:00'} {[ 49]}
{'27-Jul-2021 20:00:00'} {[123]}
{'28-Jul-2021 06:00:00'} {[ 21]}
{'29-Jul-2021 13:00:00'} {[ 66]}

4 件のコメント

That is probably about the best you can do.
In case anyone wants to experiment,
a = {
'22-Jul-2021 17:00:00', 49
[], []
[], []
[], []
[], []
'27-Jul-2021 20:00:00', 123
'28-Jul-2021 06:00:00', 21
'29-Jul-2021 13:00:00', 66 }
a = 8×2 cell array
{'22-Jul-2021 17:00:00'} {[ 49]} {0×0 double } {0×0 double} {0×0 double } {0×0 double} {0×0 double } {0×0 double} {0×0 double } {0×0 double} {'27-Jul-2021 20:00:00'} {[ 123]} {'28-Jul-2021 06:00:00'} {[ 21]} {'29-Jul-2021 13:00:00'} {[ 66]}
Sim
Sim 2022 年 8 月 11 日
Thanks a lot @Walter Roberson, very nice :-)
Jan
Jan 2022 年 8 月 11 日
As long, as the cell does not contain strings (char vectors are okay), cellfun('isempty', a) is faster than using @isempty.
Sim
Sim 2022 年 8 月 11 日
thanks a lot @Jan ! :-)

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

 採用された回答

Matt J
Matt J 2022 年 8 月 11 日

0 投票

a = {
'22-Jul-2021 17:00:00', 49
'', []
'', []
'', []
'', []
'27-Jul-2021 20:00:00', 123
'28-Jul-2021 06:00:00', 21
'29-Jul-2021 13:00:00', 66 }
a = 8×2 cell array
{'22-Jul-2021 17:00:00'} {[ 49]} {0×0 char } {0×0 double} {0×0 char } {0×0 double} {0×0 char } {0×0 double} {0×0 char } {0×0 double} {'27-Jul-2021 20:00:00'} {[ 123]} {'28-Jul-2021 06:00:00'} {[ 21]} {'29-Jul-2021 13:00:00'} {[ 66]}
a= table2cell(rmmissing(cell2table(a)))
a = 4×2 cell array
{'22-Jul-2021 17:00:00'} {[ 49]} {'27-Jul-2021 20:00:00'} {[123]} {'28-Jul-2021 06:00:00'} {[ 21]} {'29-Jul-2021 13:00:00'} {[ 66]}

3 件のコメント

Matt J
Matt J 2022 年 8 月 11 日
This was meant to show that you really should be using tables.
Sim
Sim 2022 年 8 月 11 日
編集済み: Sim 2022 年 8 月 11 日
@Matt J, cool, thanks a lot!
@Stephen23, many thasnks :-)

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeCharacters and Strings についてさらに検索

質問済み:

Sim
2022 年 8 月 11 日

編集済み:

Sim
2022 年 8 月 11 日

Community Treasure Hunt

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

Start Hunting!

Translated by