applying isempty to property of object that reside in array- without loops

4 vues (au cours des 30 derniers jours)
Mario Chang
Mario Chang le 24 Fév 2012
I have a class with only one property (named 'value'). I can instantiate an array like this:
objarray(1,10)=myclass()
which will create 10 objects of myclass with emty 'value' property. Next I set the 'value' property of the first and last object to 99.
objarray(1).value=99; objarray(end).value=99;
My question is: How do I get a logical array denoting an empty 'value' property without using loops.
Something akin to: j= isempty(objarray.value)

Réponses (1)

Walter Roberson
Walter Roberson le 24 Fév 2012
arrayfun( @(IDX) isempty(objarray(IDX).value), 1:length(objarray))
However, this does use a loop; it is just syntactically hidden. It will not be faster than using a loop.
Hmmm... Try this:
cellfun('isempty', {objarray.value})
This will use a loop internally; it is just syntactically hidden. It might be faster than using a loop yourself.
  1 commentaire
Mario Chang
Mario Chang le 24 Fév 2012
Thanks Walter,
the cellfun option does give the results I'm looking for. The big problem with that approach is that the data will be duplicated (internally by Matlab, when it makes the cell array). The 'value' property may be hundreds of thousands in length, and the array may have thousands of objects, so this may be too expensive.
I will try the arrayfun option.
Thanks again!!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by