From: Sean Douglas on
"Christopher " <christopher.badman(a)au.saabgroup.com> wrote in message <i0jo6b$nis$1(a)fred.mathworks.com>...
> "Sean Douglas" <seanjdouglas(a)hotmail.com> wrote in message <i0jh93$32c$1(a)fred.mathworks.com>...
> > hey guys i am having trouble pre-allocating memory. I am trying to follow directions i found on the internet but my code seems to be more complicated then their example. below i will show the more relevant part of my code and i will use: %%% to denote the important lines i am having problems with.
> >
> >
> > corr2=[t,s,s2];
> > La=100;
> > corrmat2plot=[]; %%% needs to also be preallocated i think
> > n=size(corr2,1)
> > cormat2plot=zeros(1,530) %%% trying to preallocat
> > corrmat2=zeros(1,530) %%% trying to preallocat
> >
> > for i=[La:n]
> > corr3=corr2(i-(La-1):i,2:end);
> >
> > me=mean(corr3);
> > me(ones(La,1),:);
> > corr3=corr3-me(ones(La,1),:);
> > covmat=corr3'*corr3./La;
> > s=sqrt(diag(covmat));
> > corrmat1=covmat./(s*s');
> > corrmat=tril(corrmat1,-1);
> > corrmat2=corrmat(2,1) %%%need to preallocate
> > corrmat2plot(end+1)=corrmat2 %%%need to preallocate
> > end
> >
> > x=[La:1:n];
> > plot(x,corrmat2plot,'r')
> >
> >
> > can someone tell me what is wrong with the way i am preallocaing? i have also seen an example where someone uses cell( , ) to preallocate rather then zeros( ,), but this did not help me.
> >
> > I think one of my problems might be that because i am using corrmat2plot(end+1) my collums not only grow each iteration but they start at the beginning all over again and grow one column longer after every new iteration.
> >
> > please some help with understanding this
>
> Hey Sean,
>
> It's hard to understand what your algorithm is actually intended to do but I'll try and give you a brief run-down of preallocation. One way to fill an array is to start with an empty array:
>
> newArray = []; % this array has no elements
>
> Then cycle through each element and expand the array by one each time by concatenation:
>
> N = 12;
> for i = 1:N
> newArray = [newArray i];
> end
>
> But this is slow and poor practice, as each newArray is updated all the contents of its previous state must be copied to a new memory location.
> More efficient, if you know how large your array will be then you can define where in memory the elements will go prior to inserting them - this is preallocation and for arrays of numerical type is typically done as you have used:
>
> N = 12;
> newArray = zeros(1,N);
>
> Now you can reference the particular element when populating:
>
> for i = 1:N
> newArray(i) = i;
> end
>
> Now to your code. You've defined the array 'corrmat2' as a 1x530 array. But then you say:
>
> corrmat2 = corrmat(2,1);
>
> which is a single value. 'corrmat2' is no longer an array, but a single numerical value. I think you want to say something more like:
>
> corrmat2(i) = corrmat(2,1);
>
> so that at the end of your loop you have an array of corrmat2 which has values of corrmat(2,1) for each iteration. Interestingly, the iterator, i, begins at La = 100 so if you want corrmat2 to begin at 1 then it will be more like:
>
> corrmat2(La - i + 1) = corrmat(2,1);
>
> similarly, you have:
>
> corrmat2plot(end+1) = corrmat2;
>
> What 'end' does is finds the index of the last element of the array. And because you haven't specified which part of 'corrmat2' should be inserted into here, you're copying the ENTIRE array of corrmat2 over to corrmat2plot(end+1:end+1+length(corrmat2)) - hope that's not too confusing!
>
> So really, using (end+1) isn't taking advantage of the preallocation, because you've only preallocated up to 'end' spaces of memory.
>
> So then, perhaps you meant:
>
> corrmat2plot(i) = corrmat2(i);
>
> or,
>
> corrmat2plot(La - i + 1) = corrmat2(i);
>
> The cell() function seems no good for this application. The cell() allows you to preallocate memory for cell arrays.
>
> Hope this helps and didn't just confuse you more!
>
> Chris

Chirs i just wanted to let you know your expanation on preallocation helped me a lot!
Thanks a lot!
 | 
Pages: 1
Prev: regexprep tokens
Next: optimization