Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Speeding Up Code

Subject: Speeding Up Code

From: Yvette Hammond

Date: 7 Jun, 2011 03:45:07

Message: 1 of 3

Hi,

I have the folllowing type of loop several times in my code and three of the lines are slowing my code down massively:

B=zeros(length(ID),1);
r_i=zeros(1,1);
for i=1:1:length(ID)

r_i=find(mat1(:,1)==ID(i,1));
if isempty(r_i)
      B(i,1)=0;
      
   else
       
       B(i,1) = r_i;
       
       if max(B)==0
           B1=0;
       else
       B1=B(B~=0);
       end
end
end

The <mx1> matrix 'ID' contains a list of numbers that I want to search for in the <px2> matrix 'mat1'. I want B1 to be an ordered list of the rows in 'mat1' where each number in 'ID' occurs (if it occurs at all). All numbers in 'mat1(:,1)' are unique and occur only once, the numbers in 'ID' may repeat. 'mat1' may not contain every number in 'ID'.

The three lines that are taking the longest to compute are (in order of longest to shortest) 'B1=B(B~=0);', 'r_i=find(mat1(:,1)==ID(i,1));' and 'if max(B)==0'.

Any ideas on how to speed up the above code would be very much appreciated, I have been struggling with this for a long time!

Thanks in advance,

Yvette

Subject: Speeding Up Code

From: Roger Stafford

Date: 7 Jun, 2011 15:51:20

Message: 2 of 3

"Yvette Hammond" wrote in message <isk6s3$r2a$1@newscl01ah.mathworks.com>...
> Hi,
>
> I have the folllowing type of loop several times in my code and three of the lines are slowing my code down massively:
>
> B=zeros(length(ID),1);
> r_i=zeros(1,1);
> for i=1:1:length(ID)
>
> r_i=find(mat1(:,1)==ID(i,1));
> if isempty(r_i)
> B(i,1)=0;
>
> else
>
> B(i,1) = r_i;
>
> if max(B)==0
> B1=0;
> else
> B1=B(B~=0);
> end
> end
> end
>
> The <mx1> matrix 'ID' contains a list of numbers that I want to search for in the <px2> matrix 'mat1'. I want B1 to be an ordered list of the rows in 'mat1' where each number in 'ID' occurs (if it occurs at all). All numbers in 'mat1(:,1)' are unique and occur only once, the numbers in 'ID' may repeat. 'mat1' may not contain every number in 'ID'.
>
> The three lines that are taking the longest to compute are (in order of longest to shortest) 'B1=B(B~=0);', 'r_i=find(mat1(:,1)==ID(i,1));' and 'if max(B)==0'.
>
> Any ideas on how to speed up the above code would be very much appreciated, I have been struggling with this for a long time!
>
> Thanks in advance,
>
> Yvette
- - - - - - - - -
  The five-line if-else-end sequence involving B1 looks completely misplaced. It keeps overwriting all its previous computations in the for-loop, so therefore it ought to be done after the for-loop finishes where it would only be performed once.

  However, there is a function, 'ismember', that can calculate B much more efficiently:

 [t,B] = ismember(ID,mat1(:,1));
 B1 = sort(B(t));

I have interpreted your phrase "I want B1 to be an ordered list" to mean you wanted the B1 indices of B to be in ascending order. That would not be accomplished in your code.

Roger Stafford

Subject: Speeding Up Code

From: Yvette Hammond

Date: 9 Jun, 2011 00:03:04

Message: 3 of 3

"Roger Stafford" wrote in message <islhdo$k9d$1@newscl01ah.mathworks.com>...
> The five-line if-else-end sequence involving B1 looks completely misplaced. It keeps overwriting all its previous computations in the for-loop, so therefore it ought to be done after the for-loop finishes where it would only be performed once.
>
> However, there is a function, 'ismember', that can calculate B much more efficiently:
>
> [t,B] = ismember(ID,mat1(:,1));
> B1 = sort(B(t));
>
> I have interpreted your phrase "I want B1 to be an ordered list" to mean you wanted the B1 indices of B to be in ascending order. That would not be accomplished in your code.
>
> Roger Stafford

Hi Roger,

Many thanks for your response! The 'ismember' function is exactly what I was looking for, the code is a lot faster now. Actually I meant that I wanted B1 to be in the order that the numbers occur in 'ID', not in ascending order, so I have just used B1=B(t) instead of B1=sort(B(t)).

Thanks again,

Yvette

Tags for this Thread

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.

Contact us