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

4 ビュー (過去 30 日間)
Mario Chang
Mario Chang 2012 年 2 月 24 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by