Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

any method to speed up matlab computation

Status
Not open for further replies.

yingyang

Full Member level 2
Full Member level 2
Joined
Dec 26, 2002
Messages
126
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
862
Whenever i used for loop in for loop in matlab, it is too slow....anyone can help?
 

LOOP in Matlab is slow in its nature.

-Try to use function provided by matlab as much as possible, since some of them are not implemented as mfiles but as dll's. It will run a lot faster than trying to implement it with elementary matlab operations.

-Or what you can do is to replace loop with some matrix operations if possible. If it is not possible and it is unacceptably slow, write it in C or C++ and make function calls to matlab when necessary(or the other way around implement loop in C/C++ and having matlab make function calls.)
 

Try not to use nested loop in for loop. It's very slow
 

One thing to remember about MATLAB is that loops make your programs run extremely slow. Especially if they are nested.

Aviod using loops and go for some sort of matrix manipulation techniques. If u want to access a certain element in a specific row in a matrix for example.....dont put it in a for loop and access each row one by one......the loops will slow down the entire process especially if your matrix is a big one. Try acessing that particular element directly.Hope this helps.

Aircraft Maniac
 

Best way of course is to avoid loops. But you must have this in mind: Matlab is a matrix oriented tool. So the more you use matrixes the more you speed up the computations. Try to convert a loop using matixes ie by calling a matrix instead getting in a lopp. The other thing for speeding up computation is to define the dimentions of the matrix. Don't let Matlab compute the dimension. Define a matrix ie M(3,4) whenever you want to use it. I think these will help. Of course you can allways use C++ : Matlab is build it on C++ packages. So, if you can't avoid loops, then it is better to use C++. In my case, a programm in Matlab wanted ~2h to run and gave me the 1/5 of the results than the same programm in C++ with the speed of ..5min!!! It is up to you my boy!
D.
 

In MATLAB programming there is a very important rule: Whenever the loop counter is used just as a counter and is not involved in the computations inside the loop, the loop can be replaced by Matrix operation
for example
x = 101:200
for n = 1:100
y[n] = x[n] ^ 2;
end

the loop can be replaced by
y = x.^2;
 

Because Matlab is an interpreted language, it must interpret every line in a for loop each time it goes through the loop. This makes Matlab painfully slow at doing loops.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top