×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Are you an
Engineering professional?
Join Eng-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Code takes too long to run

## Code takes too long to run

(OP)
I am fairly new to matlab, and I have written some code to process some image data.  My code works fine, except one of the early steps takes an extremely long time to compute.  I have a 3 column matrix with approximately 1.4 million rows and I am performing the following code on that matrix:

k=1;
SIZE0=size(Scan);
N=SIZE0(1,1);
for i=1:N
if (Scan(i,1)>=minx) && (Scan(i,1)<=maxx) && (Scan(i,2)>=miny) && (Scan(i,2)<=maxy)
Scanx(k,1)=Scan(i,1);
Scanx(k,2)=Scan(i,2);
Scanx(k,3)=Scan(i,3);
k=k+1;
end
end

I couldn't think of another way to write this for loop, but its too slow.  Is there a faster way?  Thank you for your help.

-Michael

### RE: Code takes too long to run

Scanx(k,1)=Scan(i,1);
Scanx(k,2)=Scan(i,2);
Scanx(k,3)=Scan(i,3);

could be written more concisely, see the help intro. I doubt that will have much bearing on the speed of the thing though.

breaking the IF up into a cascade of 4 IFs MAY be faster, depending on how Matlab evaluates IF. I expect the Help explains that as well.

Cheers

Greg Locock

SIG:Please see FAQ731-376: Eng-Tips.com Forum Policies for tips on how to make the best use of Eng-Tips.

### RE: Code takes too long to run

- It seems to me that i and k will always be the same.

- Scanx() is growing each time you add a new row.  If you pre-allocate before you start the loop, you'll see a big speedup

Scanx=zeros(size(Scan);

% rest of code.

-----------

However, since you are using Matlab, why not use it in anger:

- Look up "logical indexing".  You can use it to eliminate the if() test.

- Think about vectorizing this.  If can operate on the entire Scan matrix in one go, it should speed things up.

- Steve

### RE: Code takes too long to run

-Mike,

I would try to write a simply logic function, say:

function [output]=logic(x,y,minx,maxx,miny,maxy)
if(x>=minx) && (x<=maxx) && (y>=miny) && (y)<=maxy)
output=1
else
output=0
end

Then you can run your m-file:

for i=1:size(Scan,1)
temp(i,1)=logic(Scan(i,1),Scan(i,2),minx,maxx,miny,maxy)
end

Scanx=Scan(find(temp),:)

You can use the following to determine how long your analysis is taking:

% At the first command line

tic;fprintf('Analysis time...');

% At the final command line

t=toc;fprintf('%5.2f sec\n',t);

### RE: Code takes too long to run

I wasn't going to give the whole answer away, but since asixth has tried, here's my approach.  It may not be exactly what you want, but it does demonstrate some Matlab fundamentals.

% Just in case
clear Scanx

% Locate which rows to copy
idx = Scan(:,1)>=minx & Scan(:,1)<=maxx & Scan(:,2)>=miny & Scan(:,2)<=maxy;

% Copy them
Scanx(idx,:)=Scan(idx,:)

- Steve

### RE: Code takes too long to run

... the above takes about 0.5 seconds on my dog of a PC, using rand(14000000,3) for Scan.

- Steve

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Close Box

# Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!