Hello, I've got a cell array, lets say:
R =
[ 691x2 double]
[]
[]
[]
[]
[ 12x2 double]
[ 11x2 double]
[]
[]
[ 112x2 double]
[ 13x2 double]
Does anyone know of a quick way I could remove the non-zero elements of the cell array? This cell array will be a different length and will have a different number of non-zero elements each time, dependent on another loop
Thanks, Jim

 採用された回答

Aurelien Queffurust
Aurelien Queffurust 2012 年 1 月 25 日

53 投票

To remove empty cell array contents (title of your question) :
R(~cellfun('isempty',R))

12 件のコメント

Aurelien Queffurust
Aurelien Queffurust 2012 年 1 月 25 日
Note : Using isempty between quotes is faster than using function handles @isempty
Jim O'Doherty
Jim O'Doherty 2012 年 1 月 25 日
Excellent, makes me feel a little stupid for asking, but thank you very much
Jim
David Young
David Young 2012 年 1 月 25 日
In 2011b, the documentation for cellfun only mentions the string syntax under "backward compatibility".
Chris Volpe
Chris Volpe 2012 年 10 月 5 日
Why is using a quoted function name faster than using a function handle? I would have thought that the first thing cellfun did with a quoted name would be to parse it and cache a function handle.
Jan
Jan 2012 年 10 月 5 日
Here 'isempty' is not a quoted function name, but a simple string. The C-implementation of CELLFUN checks the strings mentioned in the documentation and performs the corresponding operations directly. This is much faster than calling a Matlab function for each cell element.
It is very sad, that the fast and efficient string commands are mentioned in the docs as "backward compatibility" only.
Btw, the C-source has been included in the toolbox functions of Matlab 6.5. It was a very good example for the efficient processing of cell elements in a Mex function.
Daniel Bridges
Daniel Bridges 2017 年 3 月 1 日
>> data(~structfun('isempty',data))
Error using structfun
First input must be a function handle.
Stephen23
Stephen23 2017 年 3 月 2 日
編集済み: Stephen23 2017 年 3 月 2 日
@Daniel Bridges: that syntax is only in the cellfun documentation. It is not supported by other functions.
Thulfiqar Hussein
Thulfiqar Hussein 2017 年 3 月 9 日
R = R(~cellfun('isempty',R))
Prateek Srivastava
Prateek Srivastava 2017 年 3 月 9 日
Not able to find out
Prateek Srivastava
Prateek Srivastava 2017 年 3 月 9 日
How to remove null values from cell
Chintan Modi
Chintan Modi 2017 年 5 月 9 日
編集済み: Chintan Modi 2017 年 5 月 9 日
I am also suffering from this problem. I want to delete the empty cells from this "sf" variable (i.e. shown in picture). I wish to delete it from variable so that whenever I try to find column size of the particular row; i'll be able to get only the matrix of that number cell and don't want to count the empty cell. for e.g. if i write
size(sf(3,:),2)
ans i want i.e. 5
but here it shows the 31
can someone help me to solve this problem?
Jan
Jan 2017 年 5 月 9 日
@Chintan Modi: Use the flags to catch the attraction to inapropriate messages, not for posting data concerning your problem.
Please do not attach a new question as a comment to an existing answer. Open a new thread instead.
There is no picture. What is the type of your variable sf? size(sf(3,:),2) replies the size of the array and does not care about the contents of the cell elements.
If you want to count the non-empty cells, use:
sum(~cellfun('isempty', sf(3, :))

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

その他の回答 (1 件)

David Young
David Young 2012 年 1 月 25 日

10 投票

Rnew = R(~cellfun(@isempty, R))

3 件のコメント

Harold
Harold 2012 年 4 月 25 日
I'm also trying to remove empty cells from a matrix but I can't figure out how to maintain the order for each row and column.
D=
{[-24.7697910000000,-15.8191235000000],[],[-12.6771670000000,20.4587480000000],[];[],[-20.6771670000000,-3.54125200000000],[-11.9803417500000,-14.5401785500000],[];[],[-20.6771670000000,-3.54125200000000],[4.32283300000000,-1.04125200000000],[];[],[13.0196582500000,-12.0401785500000],[],[-24.7697910000000,-15.8191235000000];[-11.9803417500000,-14.5401785500000],[],[4.32283300000000,-1.04125200000000],[];[],[-12.6771670000000,20.4587480000000],[],[13.0196582500000,-12.0401785500000]}:
I need
[-24.7697910000000,-15.8191235000000] [-12.6771670000000,20.4587480000000]
[-20.6771670000000,-3.54125200000000] [-11.9803417500000,-14.5401785500000]
[-20.6771670000000,-3.54125200000000] [4.32283300000000,-1.04125200000000]
[13.0196582500000,-12.0401785500000] [-24.7697910000000,-15.8191235000000]
[-11.9803417500000,-14.5401785500000] [4.32283300000000,-1.04125200000000]
[-12.6771670000000,20.4587480000000] [13.0196582500000,-12.0401785500000]
Andrei Bobrov
Andrei Bobrov 2012 年 4 月 25 日
D1 = D.';
out = reshape(D1(~cellfun(@isempty,D1)),2,[])';
Daniel Bridges
Daniel Bridges 2017 年 3 月 1 日
>> data(~structfun(@isempty,data))
Error using structfun
Inputs to STRUCTFUN must be scalar structures.

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

カテゴリ

ヘルプ センター および File Exchange で Structures についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by