Thread Subject:
Speeding up a matlab code

Subject: Speeding up a matlab code

From: Yong Hoon

Date: 21 Jul, 2012 13:59:13

Message: 1 of 3

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
for j= 2*NC+1:2*NT
KII(i-2*NC,j-2*NC)=K(i,j);
end
end

Subject: Speeding up a matlab code

From: dpb

Date: 21 Jul, 2012 18:18:51

Message: 2 of 3

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);

--

Subject: Speeding up a matlab code

From: Yong Hoon

Date: 22 Jul, 2012 21:25:18

Message: 3 of 3

dpb <none@non.net> wrote in message <juerqc$653$1@speranza.aioe.org>...
> 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);
>
> --

Thanks a lot!

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us