필터 지우기
필터 지우기

How to efficiently preallocate an array of handle objects

조회 수: 23 (최근 30일)
Herbert
Herbert 2015년 3월 30일
댓글: broken_arrow 2023년 3월 11일
Hi, I'm looking a fast and proper method to preallocate an array of objects, where the corresponding class is a handle class.
So far, I came across the following methods. But I'm not sure whether all of them are really preallocating the needed space.
1) Is this really preallocating the needed space?
x=myClass.empty(1e5,0);
2)
x(1:1e5) = myClass( param1, param2 );
3) I think this only work if the constructor accept nargin=0, right?
x(1e5) = myClass( param1, param2 );
4) In the case the class also inherits from matlab.mixin.Copyable
x= repmat( copy( myClass( param1, param2), 1e5,1 );

답변 (1개)

Disciple of the Forbidden Spell
Disciple of the Forbidden Spell 2018년 3월 19일
To preallocate the object array, assign the last element of the array first. MATLAB® fills the first to penultimate array elements with default ObjectArray objects.
  댓글 수: 2
Guillaume
Guillaume 2018년 3월 19일
This is option #3 in the question. As the OP stated, it only works if the constructor is designed to accept 0 arguments.
Furthermore, the behaviour for handle object is far from ideal. You'll end up with an array of n-1 duplicate handles and a different handle at the end, instead of n different handles, as explained in the doc.
broken_arrow
broken_arrow 2023년 3월 11일
So which method would be considered best practice in this case?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by