Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Returning a Logical 1 or 0 for cellfun ?

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Lin

Date: 17 Feb, 2009 02:27:01

Message: 1 of 13

for example:

a={ [] [1;1] [2;2] };

How can i return a logical 1 if a is not empty?

I used (~cellfun(@isempty, a)) but i got

ans = 0 1 1;

my expected result should be a logical 1 or a logical 0. Thanks.

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Matt Fig

Date: 17 Feb, 2009 02:37:01

Message: 2 of 13

Use all.

a={ [] [1;1] [2;2] };
all(cellfun(@isempty,a))

b = {[],[],[]}
all(cellfun(@isempty,b))




5dkb\^\ctijjz\o)]iqgfHz\zjj"oh\`j"@]\`zz`kcdoppzhg\c^T;zzj`

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Doug Schwarz

Date: 17 Feb, 2009 02:51:44

Message: 3 of 13

In article <gnd79l$7hm$1@fred.mathworks.com>,
 "Lin " <shenqlv@yahoo.com> wrote:

> for example:
>
> a={ [] [1;1] [2;2] };
>
> How can i return a logical 1 if a is not empty?
>
> I used (~cellfun(@isempty, a)) but i got
>
> ans = 0 1 1;
>
> my expected result should be a logical 1 or a logical 0. Thanks.


What you did is correct. MATLAB displays true and false as 1 and 0.
Check the class of the return variable.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Lin

Date: 17 Feb, 2009 12:54:02

Message: 4 of 13

Doug Schwarz <see@sig.for.address.edu> wrote in message <see-E216E5.21514416022009@news.frontiernet.net>...
> In article <gnd79l$7hm$1@fred.mathworks.com>,
> "Lin " <shenqlv@yahoo.com> wrote:
>
> > for example:
> >
> > a={ [] [1;1] [2;2] };
> >
> > How can i return a logical 1 if a is not empty?
> >
> > I used (~cellfun(@isempty, a)) but i got
> >
> > ans = 0 1 1;
> >
> > my expected result should be a logical 1 or a logical 0. Thanks.
>
>
> What you did is correct. MATLAB displays true and false as 1 and 0.
> Check the class of the return variable.
>

i dont really understand the solution. Perhaps i do a little elaboration on my problem.

Suppose i have something as shown:

%data
a={ [] [1;1] [2;2] };
% if - else statement
if (a is empty)
disp('a is empty')
else
disp('a is not empty')

expected result
'a is not empty'

How can i achieve the expected result? Thanks in advance.

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Bruno Luong

Date: 17 Feb, 2009 13:00:04

Message: 5 of 13

"Lin " <shenqlv@yahoo.com> wrote in message <gnec19$bi6$1@fred.mathworks.com>...
> Doug Schwarz <see@sig.for.address.edu> wrote in message <see-E216E5.21514416022009@news.frontiernet.net>...
> > In article <gnd79l$7hm$1@fred.mathworks.com>,
> > "Lin " <shenqlv@yahoo.com> wrote:
> >
> > > for example:
> > >
> > > a={ [] [1;1] [2;2] };
> > >
> > > How can i return a logical 1 if a is not empty?
> > >
> > > I used (~cellfun(@isempty, a)) but i got
> > >
> > > ans = 0 1 1;
> > >
> > > my expected result should be a logical 1 or a logical 0. Thanks.
> >
> >
> > What you did is correct. MATLAB displays true and false as 1 and 0.
> > Check the class of the return variable.
> >
>
> i dont really understand the solution. Perhaps i do a little elaboration on my problem.
>
> Suppose i have something as shown:
>
> %data
> a={ [] [1;1] [2;2] };
> % if - else statement
> if (a is empty)
> disp('a is empty')
> else
> disp('a is not empty')
>
> expected result
> 'a is not empty'
>
> How can i achieve the expected result? Thanks in advance.

Perhaps:

aisempty = isempty(a)

Bruno

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Jos

Date: 17 Feb, 2009 13:03:02

Message: 6 of 13

"Lin " <shenqlv@yahoo.com> wrote in message <gnec19$bi6$1@fred.mathworks.com>...
> Doug Schwarz <see@sig.for.address.edu> wrote in message <see-E216E5.21514416022009@news.frontiernet.net>...
> > In article <gnd79l$7hm$1@fred.mathworks.com>,
> > "Lin " <shenqlv@yahoo.com> wrote:
> >
> > > for example:
> > >
> > > a={ [] [1;1] [2;2] };
> > >
> > > How can i return a logical 1 if a is not empty?
> > >
> > > I used (~cellfun(@isempty, a)) but i got
> > >
> > > ans = 0 1 1;
> > >
> > > my expected result should be a logical 1 or a logical 0. Thanks.
> >
> >
> > What you did is correct. MATLAB displays true and false as 1 and 0.
> > Check the class of the return variable.
> >
>
> i dont really understand the solution. Perhaps i do a little elaboration on my problem.
>
> Suppose i have something as shown:
>
> %data
> a={ [] [1;1] [2;2] };
> % if - else statement
> if (a is empty)
> disp('a is empty')
> else
> disp('a is not empty')
>
> expected result
> 'a is not empty'
>
> How can i achieve the expected result? Thanks in advance.

I think we are talking about different kinds of empty

A = {}
B = {[]}
C = {[] [] []}
D = {[] 1 2}

isempty(A) % true
isempty(B) % false
isempty(D) % false
isempty(C) %false

cellfun('isempty',A) % empty!
cellfun('isempty',B) % true
cellfun('isempty',C) % [true true true]
cellfun('isempty',D) % [true false false]

What do you(!) mean by being empty?

Jos

Subject: Returning a Logical 1 or 0 for cellfun ?

From: us

Date: 17 Feb, 2009 14:31:01

Message: 7 of 13

"Jos"
> What do you(!) mean by being empty...

most likely: if he/she looks into his/her pocketbook...

us

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Lin

Date: 17 Feb, 2009 15:07:02

Message: 8 of 13

"Jos " <#10584@fileexchange.com> wrote in message <gneci6$guv$1@fred.mathworks.com>...
> "Lin " <shenqlv@yahoo.com> wrote in message <gnec19$bi6$1@fred.mathworks.com>...
> > Doug Schwarz <see@sig.for.address.edu> wrote in message <see-E216E5.21514416022009@news.frontiernet.net>...
> > > In article <gnd79l$7hm$1@fred.mathworks.com>,
> > > "Lin " <shenqlv@yahoo.com> wrote:
> > >
> > > > for example:
> > > >
> > > > a={ [] [1;1] [2;2] };
> > > >
> > > > How can i return a logical 1 if a is not empty?
> > > >
> > > > I used (~cellfun(@isempty, a)) but i got
> > > >
> > > > ans = 0 1 1;
> > > >
> > > > my expected result should be a logical 1 or a logical 0. Thanks.
> > >
> > >
> > > What you did is correct. MATLAB displays true and false as 1 and 0.
> > > Check the class of the return variable.
> > >
> >
> > i dont really understand the solution. Perhaps i do a little elaboration on my problem.
> >
> > Suppose i have something as shown:
> >
> > %data
> > a={ [] [1;1] [2;2] };
> > % if - else statement
> > if (a is empty)
> > disp('a is empty')
> > else
> > disp('a is not empty')
> >
> > expected result
> > 'a is not empty'
> >
> > How can i achieve the expected result? Thanks in advance.
>
> I think we are talking about different kinds of empty
>
> A = {}
> B = {[]}
> C = {[] [] []}
> D = {[] 1 2}
>
> isempty(A) % true
> isempty(B) % false
> isempty(D) % false
> isempty(C) %false
>
> cellfun('isempty',A) % empty!
> cellfun('isempty',B) % true
> cellfun('isempty',C) % [true true true]
> cellfun('isempty',D) % [true false false]
>
> What do you(!) mean by being empty?
>
> Jos

a={ [] [1;1] [2;2] };
My "empty" is to check if there is any content in a.
from the example, a has 2 content, therefore is not an empty(refers to []) cell.

if (there is content in a)
disp('there is some content in a')
else
disp('there is no content in a')

sorry about that. i hope my description is clear enough this time.

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Jos

Date: 17 Feb, 2009 15:31:01

Message: 9 of 13

"Lin " <shenqlv@yahoo.com> wrote in message <gnejqm$dku$1@fred.mathworks.com>...
> "Jos " <#10584@fileexchange.com> wrote in message <gneci6$guv$1@fred.mathworks.com>...
..
> > A = {}
> > B = {[]}
> > C = {[] [] []}
> > D = {[] 1 2}
> >
> > isempty(A) % true
> > isempty(B) % false
> > isempty(D) % false
> > isempty(C) %false
> >
> > cellfun('isempty',A) % empty!
> > cellfun('isempty',B) % true
> > cellfun('isempty',C) % [true true true]
> > cellfun('isempty',D) % [true false false]

 
> a={ [] [1;1] [2;2] };
> My "empty" is to check if there is any content in a.
> from the example, a has 2 content, therefore is not an empty(refers to []) cell.


Which one of my examples (variable 'A', 'B', 'C', or 'D) do you think most similar to your example (variable 'a')?

Should the "test_for_empty" function return true or false?

Can the function ALL help you?

Jos

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Matt Fig

Date: 17 Feb, 2009 16:05:05

Message: 10 of 13

Wasn't 'all' mentioned in the very first response to your post?

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Lin

Date: 17 Feb, 2009 16:09:03

Message: 11 of 13

"Jos " <#10584@fileexchange.com> wrote in message <gnel7l$33u$1@fred.mathworks.com>...
> "Lin " <shenqlv@yahoo.com> wrote in message <gnejqm$dku$1@fred.mathworks.com>...
> > "Jos " <#10584@fileexchange.com> wrote in message <gneci6$guv$1@fred.mathworks.com>...
> ..
> > > A = {}
> > > B = {[]}
> > > C = {[] [] []}
> > > D = {[] 1 2}
> > >
> > > isempty(A) % true
> > > isempty(B) % false
> > > isempty(D) % false
> > > isempty(C) %false
> > >
> > > cellfun('isempty',A) % empty!
> > > cellfun('isempty',B) % true
> > > cellfun('isempty',C) % [true true true]
> > > cellfun('isempty',D) % [true false false]
>
>
> > a={ [] [1;1] [2;2] };
> > My "empty" is to check if there is any content in a.
> > from the example, a has 2 content, therefore is not an empty(refers to []) cell.
>
>
> Which one of my examples (variable 'A', 'B', 'C', or 'D) do you think most similar to your example (variable 'a')?
>
> Should the "test_for_empty" function return true or false?
>
> Can the function ALL help you?
>
> Jos

If a is something dynamic, that means to say, a has nothing in it initially, but probably after some loops, a will have some content in it.

Example:
a = {};
after some loops
a = {[] [1;2] [3;4]};

am i right to write the following codes?

if (isempty(a)== 1)
disp('Nothing can be found in a');
else
disp('There is some content in a');

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Matt Fig

Date: 17 Feb, 2009 16:24:01

Message: 12 of 13

"Lin " <shenqlv@yahoo.com> wrote in message <gnenev$l9e$1@fred.mathworks.com>...
> if (isempty(a)== 1)
> disp('Nothing can be found in a');
> else
> disp('There is some content in a');



It might help if you actually read the responses to you question. This is at least the third time the 'all' function is being mentioned! Jos also gave you a list of cells to choose from, since the understanding of empty for cells can be tricky. Selecting one would clarify what you want for the rest of us.


a = {[],[],[]};

if all(cellfun(@isempty,a))
disp('a is empty')
else
disp('a is not empty')
end



a = {[],[3],[]};

if all(cellfun(@isempty,a))
disp('a is empty')
else
disp('a is not empty')
end


Note that specifically when checking in an if statement conditional, you don't even need the all because if evaluates a vector as all anyway. This just makes it clearer to the reader.





&"@"@N07&&,6&:""G@@#.-@/0G1"$6()`/@.yZ)"#*00e001@-*m")5@5$5

Subject: Returning a Logical 1 or 0 for cellfun ?

From: Lin

Date: 17 Feb, 2009 17:16:01

Message: 13 of 13

"Matt Fig" <spamanon@yahoo.com> wrote in message <gneob1$kup$1@fred.mathworks.com>...
> "Lin " <shenqlv@yahoo.com> wrote in message <gnenev$l9e$1@fred.mathworks.com>...
> > if (isempty(a)== 1)
> > disp('Nothing can be found in a');
> > else
> > disp('There is some content in a');
>
>
>
> It might help if you actually read the responses to you question. This is at least the third time the 'all' function is being mentioned! Jos also gave you a list of cells to choose from, since the understanding of empty for cells can be tricky. Selecting one would clarify what you want for the rest of us.
>
>
> a = {[],[],[]};
>
> if all(cellfun(@isempty,a))
> disp('a is empty')
> else
> disp('a is not empty')
> end

> a = {[],[3],[]};
>
> if all(cellfun(@isempty,a))
> disp('a is empty')
> else
> disp('a is not empty')
> end

> Note that specifically when checking in an if statement conditional, you don't even need the all because if evaluates a vector as all anyway. This just makes it clearer to the reader.

i am sorry about that. Thanks Jos, Matt, Bruno, Doug for all ur replies. i hope i didnt miss anyone. Thanks.

Tags for this Thread

No tags are associated with this thread.

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.

Contact us