"Peter Mairhofer" <email@example.com> wrote in message news:firstname.lastname@example.org... > ... does this always re-allocate/copy the complete vector although it is > pre-allocated? > > classdef SampledSignal > properties (GetAccess = 'public', SetAccess = 'public') > data; > end > > methods > function this = set.data(this, vector) > [...] > this.data = vector(:); > end > end > > methods > function this = SampledSignal(N) > [...] > this.data = zeros(N, 1); > end > end > > x = SampledSignal; > for n=1:N > x.data(n) = func(n); > end > > takes forever;
It calls your set.data method N times. If you had a get.data method as well, it too would be called N times. If N is large, that could take a while.
> x = SampledSignal; > data = x.data; > for n=1:N > data(n) = func(n); > end > x.data = data; > > takes "only" 30 seconds on my old machine. > Why?
This calls your set.data method 1 time on the last line and a get.data method (if you have one) 1 time on the second line.
How large a value of N are you using? How long does 1 call to set.data take? If you let it run about 30 seconds plus (N-1)*(duration of a set.data call) [for a smaller value of N] does the first code complete its execution?