Skip to Main Content Skip to Search
Login
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Thread Subject: Cells :Remove empty and size(1,1)

Subject: Cells :Remove empty and size(1,1)

From: susan

Date: 13 Feb, 2008 17:55:10

Message: 1 of 6

Hi! I have a large cell array with entries similar:
a{1}=[1 2 3 4];
a{2}=[];
a{3}=[1];
a{4}=[1 2 3 4 5 6];

What is the most efficient way to remove the empty cells (a
{2}) and the cells with only 1 value (a{3})?

THanks,
S

Subject: Cells :Remove empty and size(1,1)

From: us

Date: 13 Feb, 2008 18:25:29

Message: 2 of 6

"susan ":
<SNIP wants to remove the bad guys...

one of the many solutions

     a{1,1}=[1 2 3 4];
     a{2,1}=[];
     a{3,1}=[1];
     a{4,1}=[1 2 3 4 5 6];
     ix=cellfun(@numel,a);
     a(ix<=1)=[]

us

Subject: Cells :Remove empty and size(1,1)

From: Bruno Luong

Date: 13 Feb, 2008 19:07:13

Message: 3 of 6

in the same line:

a(cellfun(@isempty,a))=[]

Subject: Cells :Remove empty and size(1,1)

From: Arthur G

Date: 13 Feb, 2008 19:27:42

Message: 4 of 6

On Wed, 13 Feb 2008 14:07:13 -0500, Bruno Luong <b.luong@fogale.fr> wrot=
e:
> in the same line:
>
> a(cellfun(@isempty,a))=3D[]
>

That only removes empty cells. If you want to remove cells with <=3D1 =

element, use
a( cellfun(@(x)numel(x)<=3D1, a) ) =3D [];


--Arthur

Subject: Cells :Remove empty and size(1,1)

From: Bruno Luong

Date: 13 Feb, 2008 19:35:11

Message: 5 of 6

"Arthur G" <gorramfreak+news@gmail.com> wrote in message
<op.t6g9ogca1g2ca1@arthurmbp.local>...

> That only removes empty cells. If you want to remove cells
with <=3D1 =
>

Oops you are right,I should read the question more carefully.

Bruno

Subject: Cells :Remove empty and size(1,1)

From: us

Date: 13 Feb, 2008 21:15:12

Message: 6 of 6

"Arthur G":
<SNIP solution with a bad taste...

> use
> a( cellfun(@(x)numel(x)<=1, a) ) = [];

i suggest to NOT do this; rather use the first (us's)
approach as a one liner - i'll show you why:

% the data
     a{1}=[1 2 3 4];
     a{2}=[];
     a{3}=[1];
     a{4}=[1 2 3 4 5 6];
     b=a;
% ag
tic;
for i=1:10000
     a=b;
     a(cellfun(@(x) numel(x)<=1,a))=[];
end;
toc
% Elapsed time is 0.669293 seconds.
% us
tic;
for i=1:10000
     a=b;
     a(cellfun(@numel,a)<=1)=[];
end;
toc
% Elapsed time is 0.136211 seconds. % ~19%!!!!!

just a thought...
us
ps: i leave it up to you to figure out why this happens...

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
numel us 13 Feb, 2008 13:25:33
code us 13 Feb, 2008 13:25:33
cellfun us 13 Feb, 2008 13:25:33
cells susan 13 Feb, 2008 12:59:57
rssFeed for this Thread

envelope graphic E-mail this page to a colleague

Public Submission Policy
NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.
Related Topics