Object Array Access Speed

조회 수: 2 (최근 30일)
Dukwhan
Dukwhan 2011년 2월 22일
Hello everyone. I have a class, 'objContainer', that holds an object array of 'obj' handle objects. Each 'obj' holds four numbers. I want to access and edit each 'obj' individually and their numbers quickly. The code to set and retrieve the objects is as follows, but it's very slow even for sizes of 4 x 100:
function objContainer = setObj(objContainer, indices, obj)
idx = num2cell(indices);
objContainer.objArray(idx{:}) = obj;
end
function obj = getObj(objContainer, indices)
idx = num2cell(indices);
obj = objContainer.objArray(idx{:});
end
Looking at the profiler, the slowdown is at the lines:
objContainer.objArray(idx{:}) = obj;
and
obj = objContainer.objArray(idx{:});
Does anyone have a faster way of accessing these types of arrays?

채택된 답변

Walter Roberson
Walter Roberson 2011년 2월 22일
Possibly using sub2ind() might be faster?
idx = num2cell(indices);
ind = sub2ind(size(objContainer.objArray), idx{:});
and then reference objContainer.objArray(ind)
It appears from context that indices is a single vector, not an array. If so then the sub2ind() could be sped up, not needing to use cells:
s = size(objContainer.objArray);
ind = 1 + cumprod([1 s(1:end-1)]) * (indices - 1);
  댓글 수: 4
Dukwhan
Dukwhan 2011년 2월 24일
I've profiled this with an extreme case of 4 x 1000 elements and the slowdown still shows the slowdown at:
objContainer.objArray(ind) = obj;
I'm beginning to think it's more to do with the internal implementation of the MATLAB object array than anything that can be done externally. I will try my last resort with an 'eval' function and post again soon.
Dukwhan
Dukwhan 2011년 3월 9일
As this question is quite old, I'll just say that it is MATLAB's internal code that is slow. Specifically, the accessing of an element in an object array.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by