|
On 7/21/2012 8:59 AM, Yong Hoon wrote:
> I have a problem of computing speed for this code. I appreciate if
> someone helps me to reduce the computing speed.
>
> % the size of K is usually (100000x100000).
> NT = 40401;
> NC = 43;
> %% Internal Nodes
> KII=[];
> for i= 2*NC+1:2*NT % i=87:80802
> for j= 2*NC+1:2*NT % j=87:80802
> KII(i-2*NC,j-2*NC)=K(i,j);
> end
> end
Well, w/o anything else, preallocate KII instead of making it just null
thereby force reallocation on every assignment...
KII=zeros(size1,size2);
What are size1,size2 ???
Let's see...
i=2*NC+1:2*NT
KII(idx1,idx2)
idx1 = i-2*NC --> 2*NC+1-2*NC:2*NT-2*NC
= 1:2*NT-2*NC
= 1:2*(NT-NC)
idx2 = j-2*NC --> 2*NC+1-2*NC:2*NT-2*NC
= 1:2*NT-2*NC
= 1:2*(NT-NC)
So,
KII=zeros(2*(NT-NC)); % preallocate
Now, since storage order is column major, rearrange the loops...
> for j= 2*NC+1:2*NT
> for i= 2*NC+1:2*NT
> KII(i-2*NC,j-2*NC)=K(i,j);
> end
> end
But, since both indices are incremental by one, the real answer (the
MATLAB way :) ) is to write
i1=2*NC+1;
i2=2*NT;
KII=K(i1:i2,i1:i2);
--
|