MATLAB Answers


Same code works on my computer but not on my friends'

Asked by Amadeus
on 6 Dec 2012


I am having an strange problem: I have Matlab R2012a installed on my own computer and Matlab R2009a (multi thread) installed on my friend's computer. Because of the reason that my computer has only 2gb ram whereas my friend's has 128gb and a better processor, I decided to execute my code on my friend's computer.

I connected to his computer with a remote desktop connection program, downloaded my code and dataset from dropbox and executed my code. However, my code's execution did not finish even though an hour passed. (It took about 20 minutes on my computer and if you think that my friend's system is much more better than mine, it should have taken less than 20 minutes.) Then I waited a few hours more but execution did not finish.

Then, to understand where the problem is, I evaluated the code step by step and noticed that it could not finish the execution of the following loop:

for n=1:max_t
    for m=1:t(1,n).numberofPoints
minx = min(x_t(:));
miny = min(y_t(:));
minz = min(z_t(:));

It is just a simple loop to obtain my minimum point in x,y,z coordinates. I know I do not need loops to obtain maximum and minimum points of a struct but I am new to MATLAB and do not know much. Therefore it seemed like the easiest way to me.

I really have to be able to use my friends' computer because my ram will not be enough for the further process. Therefore I need to solve the problem. Could you please help me? What can be the reason that same code works on my computer but not on my friends' computer?



Why the tag, Matlab Compiler?

I thought that it could be related to the different compiler versions on different matlab versions, but of course I am not sure.

Matlab Compiler is a special product, see


No products are associated with this question.

2 Answers

Answer by per isakson
on 6 Dec 2012
Edited by per isakson
on 6 Dec 2012
 Accepted answer

R2012a might handle your code more efficiently than R2009a. There is something called "just in time compiler", JIT, which The Mathworks improve with each release. Others know more about the details. However, your code might run faster on your computer.


    if rem(n,10) == 0 
       disp( [ num2str(n), ' -- ', datestr( now ) ] )

or something similar in the outer loop to see what's going on.

BTW: are you sure ram is a bottleneck?


For my PC, bottleneck is the ram. Because I keep getting out of memory errors.

So ram is a bottleneck, nevertheless you need to analyze what's going on your friends computer. Maybe, you should try to use the function, profile. It's good.

Answer by Jan Simon
on 6 Dec 2012
Edited by Jan Simon
on 6 Dec 2012

As far as I can see you did not pre-allocate the variable x_t. Matlab 2012b has some smart methods to reduces the dramataic slowdown causes by this, but I'm not sure if this has been introduced in 2012a already.

Try this for an implicit pre-allocation:

n = sum([t.numberofPoints]);
k = n;
for n = max_t:-1:1
  for m = t(1,n).numberofPoints:-1:1
      k = k - 1;

You find a lot of explanations in this forum, when you search for "pre-allocation".


Join the 15-year community celebration.

Play games and win prizes!

Learn more
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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!