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

4 visualizaciones (últimos 30 días)
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)

Respuestas (1)

Walter Roberson
Walter Roberson el 24 de Feb. de 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 comentario
Mario Chang
Mario Chang el 24 de Feb. de 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!!

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by