Speeding up large array element access/modification in class

4 ビュー (過去 30 日間)
Richard
Richard 2011 年 3 月 24 日
Hi all,
So I have a class and one of its properties is an array which is initialised to:
large_array = zeros(1,1000000);
I also have a method in the class:
function log(this)
this.large_array(1,this.current_iteration) = this.some_value;
end
My problem is this method which updates a value in the array per iteration takes AGES (89.9% of the total program time). I am already preallocating the array so I do not understand why it is taking so long. So my questions are:
Why is this taking so long?
How can I speed the accessing/modifying up?
Thanks for your time, Richy

回答 (1 件)

Richard
Richard 2011 年 3 月 24 日
Ok I found my answer, for anyone having the same problem take a look here:
This seems to be a bug with Matlab (I'm using R2010b).
The solution is to change the assignment so that the value you want to assign to the index in the large array is stored within the method temporarily. The updated method would look like this:
function log(this)
temp = this.some_value;
this.large_array(1,this.current_iteration) = temp;
end
I hope there's a patch or Matlab R2011a fixes this issue.

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by