Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: object.vector(100) = 1
Posted:
Jan 23, 2013 10:22 AM


"Peter Mairhofer" <63832452@gmx.net> wrote in message news:kdn44k$vib$1@news.albasani.net... > ... does this always reallocate/copy the complete vector although it is > preallocated? > > 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.
http://www.mathworks.com/help/matlab/matlab_oop/propertyaccessmethods.html#brh8ht7
> 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 (N1)*(duration of a set.data call) [for a smaller value of N] does the first code complete its execution?
 Steve Lord slord@mathworks.com To contact Technical Support use the Contact Us link on http://www.mathworks.com



