Problems with Matlab Projects? You may face many Problems, but do not worry we are ready to solve your Problems. All you need to do is just leave your Comments. We will assure you that you will find a solution to your project along with future tips. On Request we will Mail you Matlab Codes for Registered Members of this site only, at free service...Follow Me.

Vectorizing loops in MATLAB


In MATLAB one should try to avoid loops. This can be done by vectorizing your code. The idea is that MATLAB is very fast on vector and matrix operations and correspondingly slow with loops. We illustrate this by an example.

EXAMPLE 1

Given an=n, and bn=1000n for n=1,...,1000. Calculate
1000
n=1
(anbn), and store in the variable ssum.
Solution: It might be tempting to implement the above calculation as
 a = 1:1000; b = 1000 - a; ssum=0; for n=1:1000 %poor style...    ssum = ssum +a(n)*b(n); end 
Recognizing that the sum is the inner product of the vectors a and b, abT, we can do better:
 ssum = a*b' %Vectorized, better! 
For more detailed information on vectorization, please take a look at MathWorks' Code Vectorization Guide.

0 comments:

Post a Comment

Recent Comments

Popular Matlab Topics

Share your knowledge - help others

Crazy over Matlab Projects ? - Join Now - Follow Me

Sites U Missed to Visit ?

Related Posts Plugin for WordPress, Blogger...

Latest Articles

Special Search For Matlab Projects

MATLAB PROJECTS

counter

Bharadwaj. Powered by Blogger.