C.Colden
C.Colden

Reputation: 627

How to find empty array in a cell array in matlab?

My data has the following structure:

Cell_Array = {{[1]},{[]},{[8]}};

How can I find the empty array in this cell array without making a double loop?

This does not work. Since everything is not empty in this cell array.

~cellfun(@isempty,Cell_Array(:))

As you can see here:

isempty(Cell_Array{1,2})
ans = 0

It only works if:

isempty(Cell_Array{1,2}{1,1})
ans = 1

How can I solve this elegantly with cellfun?

Upvotes: 1

Views: 1159

Answers (3)

Luis Mendo
Luis Mendo

Reputation: 112659

This works without relying on the array having a specific structure. It gives a logical index with true for cells that contain {[]} and false for other cells.

result = cellfun(@(x)isequal(x,{[]}), Cell_Array);

Upvotes: 2

Sam Roberts
Sam Roberts

Reputation: 24127

>> Cell_Array = {{[1]},{[]},{[8]}};
>> cellfun(@(x)isempty(x{:}),Cell_Array)
ans =
     0     1     0

Upvotes: 2

GameOfThrows
GameOfThrows

Reputation: 4510

well, one way you can do assuming that all your data are like the one in your example is:

C =  [Cell_Array{:}];
~cellfun(@isempty,C(:))

ans =

 1
 0
 1

Upvotes: 1

Related Questions