logo for matrixlab-examples.com
[?] Subscribe To This Site

XML RSS
Add to Google
Add to My Yahoo!
Add to My MSN
Subscribe with Bloglines


Home
Matrixmania Blog
Contact
-> Sitemap <-
Matlab Books
Quick Matlab Guide
Matlab Tutorials
Matlab Examples
Matlab Flow Control
Boolean Algebra
Linear Algebra
Matlab 2D Plots
Matlab 3D Plots
Matlab GUI
Matlab Cookbook I
Matlab Cookbook II
Probability and Stats
Forums and Help
Relevant Links
Fun!
Your own Website?
Terms/Policies
leftimage for matrixlab-examples.com

iteration (for loop)

The MATLAB iteration structure (for loop) repeats a group of statements a fixed, predetermined number of times. A matching end delineates the statements.

The general format is:

for variable = expression
    statement
    ...
end


Example:


c = 5;

% Preallocate matrix, fill it with zeros
a = zeros(c, c);

for m = 1 : c
    for n = 1 : c
        a(m, n) = 1/(m + n * 5);
    end
end
a



The result is:

a =
    0.1667    0.0909    0.0625    0.0476    0.0385
    0.1429    0.0833    0.0588    0.0455    0.0370
    0.1250    0.0769    0.0556    0.0435    0.0357
    0.1111    0.0714    0.0526    0.0417    0.0345
    0.1000    0.0667    0.0500    0.0400    0.0333


The semicolon terminating the inner statement suppresses repeated printing,
and the a after the loop displays the final result.

It is a good idea to indent the loops for readability, especially when they are nested.

From 'iteration' to home
From 'iteration' to 'Matlab Code'


footer for matlab for loop page