How can I find the length of a cell array in a particular dimension excluding 0x0 doubles?

As a simple example, say I had this cell array:
examplecell =
{'Name1'} {'Heights'} {'Weights'} {[20000]} {[365]} {[7]}
{'Name2'} {'Heights'} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
{'Name3'} {0x0 double} {0x0 double} {0x0 double} {0x0 double} {0x0 double}
In this 2-D array, I would want to find the number of elements in a specific row which are not {0x0 double}. It is important that the output is for the particular row, not the cell array overall e.g. for the first row the result would be 6, but for the second row it would be 2, and for the third it would be 1. In practice, the cell array is 6-D.

 Accepted Answer

You can use ~cellfun('isempty',examplecell) to find out if cells are non-empty, and then you can use sum (specifying a dimension) to count the number of non-empty cells in a particular dimension.
Edit:
This method is much faster than cellfun(@isempty,___), but it doesn't work for classes where isempty is overloaded. At least the string, table, and datetime classes are such examples, but there may be more.
examplecell = [{'Name1'} {'Heights'} {'Weights'} {[20000]} {[365]} {[7]};{'Name2'} {'Heights'} {[]} {[]} {[]} {[]};{'Name3'} {[]} {[]} {[]} {[]} {[]}]
examplecell = 3x6 cell array
{'Name1'} {'Heights' } {'Weights' } {[ 20000]} {[ 365]} {[ 7]} {'Name2'} {'Heights' } {0×0 double} {0×0 double} {0×0 double} {0×0 double} {'Name3'} {0×0 double} {0×0 double} {0×0 double} {0×0 double} {0×0 double}
fprintf('char syntax takes %.2f microseconds\n',1000*1000*timeit(@() ~cellfun('isempty',examplecell)))
char syntax takes 6.14 microseconds
fprintf('handle syntax takes %.2f microseconds\n',1000*1000*timeit(@() ~cellfun(@isempty,examplecell)))
handle syntax takes 16.01 microseconds
(on my machine the difference is even larger: 2 vs 9)

5 Comments

Hi Rik,
Thank you for your answer!
What would you suggest if the empty cells contain 0x0 strings instead of 0x0 doubles? The solution you mentioned does not work in this case.
For example:
a = ["11" "aa"];
b = '\d{2}';
regexp(a,b,'match')
Gives the following result:
ans =
1×2 cell array
{["11"]} {0×0 string}
I have been really struggling finding a way to index these 0x0 strings in cell arrays. Converting cell to strings also won't work when 0x0 string exists in the cell...
a = ["11","aa"];
b = '\d{2}';
c = regexp(a,b,'match')
c = 1x2 cell array
{["11"]} {0×0 string}
x = ~cellfun('isempty',c)
x = 1x2 logical array
1 1
y = ~cellfun(@isempty,c)
y = 1x2 logical array
1 0
An interesting inconsistency in the old 'isempty' compatibility syntax.
I'm tempted to call this inconsistency a bug.
Thanks for both of your inputs, now it works perfectly! I have no idea if it's a bug though...
I have now reported it as a bug, so we'll see what Mathworks thinks.
Update:
Since string has an overloaded isempty method this behavior is expected. I could not find where this is documented (which('isempty','-all') did suggest something is different). Support forwarded my service request to the documentation people, so they should now be aware.
I wrote the attached consistency checker to show the issue for the fundamental classes.
% NB: the timings seem to have a larger spread than on a local machine
% it took 3 tries to get time reduction figures that make sense
cellfun_consistency_checker
| Class | isempty(__) | handle syntax | legacy syntax | time reduction | | logical | true | true | true | 11% | | string | true | true | *false* | NaN% | | char | true | true | true | 50% | | int8 | true | true | true | 51% | | int16 | true | true | true | 52% | | int32 | true | true | true | 52% | | int64 | true | true | true | 50% | | uint8 | true | true | true | 49% | | uint16 | true | true | true | 49% | | uint32 | true | true | true | 50% | | uint64 | true | true | true | 49% | | single | true | true | true | 50% | | double | true | true | true | 49% | | table | true | true | *false* | NaN% | | timetable | true | true | *false* | NaN% | | cell | true | true | true | 49% | | struct | true | true | true | 51% |
% (the timetable (which fails), cell, and struct lines seem to be cropped)

Sign in to comment.

More Answers (0)

Categories

Products

Release

R2017b

Asked:

on 17 Aug 2018

Edited:

Rik
on 15 Jan 2021

Community Treasure Hunt

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

Start Hunting!