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

4 次查看(过去 30 天)
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)

回答(1 个)

Walter Roberson
Walter Roberson 2012-2-24
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 个评论
Mario Chang
Mario Chang 2012-2-24
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!!

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by