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

Learn moreOpportunities for recent engineering grads.

Apply Today**New to MATLAB?**

Asked by Amadeus
on 6 Dec 2012

Hello,

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:

l=1; for n=1:max_t for m=1:t(1,n).numberofPoints x_t(l)=t(1,n).matrix(m,1); y_t(l)=t(1,n).matrix(m,2); z_t(l)=t(1,n).matrix(m,3); l=l+1; end end

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?

Regards

*No products are associated with this question.*

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.

Put

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

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

BTW: are you sure ram is a bottleneck?

Amadeus
on 6 Dec 2012

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

per isakson
on 6 Dec 2012

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 x_t(k)=t(1,n).matrix(m,1); y_t(k)=t(1,n).matrix(m,2); z_t(k)=t(1,n).matrix(m,3); k = k - 1; end end

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

## 3 Comments

## per isakson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55882#comment_115685

Why the tag, Matlab Compiler?

## Amadeus (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55882#comment_115689

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

## per isakson (view profile)

Direct link to this comment:http://www.mathworks.com/matlabcentral/answers/55882#comment_116094

Matlab Compiler is a special product, see http://www.mathworks.se/products/compiler/.