Object Array Access Speed
2 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
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);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!