for index = values statements end |
x = 1; for n = [1 2 3] x = x + n end |
n = 1 x = 1 x = x + n x = 1 + 1 x = 2 |
n = 2 x = 2 x = x + n x = 2 + 2 x = 4 |
n = 3 x = 4 x = x + n x = 4 + 3 x = 7 |
m = 1; for A = [3 6 1 5 8] fprintf('The value of A(%d) is %.0f\n',m,A); m = m + 1; end |
b = 'Boring!'; m = 1; for A = ['B' 'o' 'o' '-' 'Y' 'a' 'h'] fprintf('%s\n',A); b(m) = A m = m + 1; end |
m = 1; for B = 1:10 fprintf('The value of B(%d) is %.0f\n',m,B); m = m + 1; end |
m = 1; for B = 20:80 fprintf('The value of B(%d) is %.0f\n',m,B); m = m + 1; end |
m = 1; for C = 1:2:10 fprintf('The value of C(%d) is %.0f\n',m,C); m = m + 1; end |
D = ones(10,1) for C = 1:2:10 D(C,1) = C; fprintf('The value of D(%d,1) is %.0f\n',C,C); D end |
m = 1; for C = 1:0.2:10 fprintf('The value of C(%d) is %.0f\n',m,C); m = m + 1; end |
m = 1; for C = 10:-1:1 fprintf('The value of C(%d) is %.0f\n',m,C); m = m + 1; end |
D = ones(10,1) for C = 10:-1:1 D(C,1) = C; fprintf('The value of D(%d,1) is %.0f\n',C,C); D end |
m = 1; for C = 10:-0.2:1 fprintf('The value of C(%d) is %.0f\n',m,C); m = m + 1; end |
m = 1; E = zeros(4); for D = rand(4) E(:,m) = D; disp('Column vector') disp(D) E m = m + 1; end |
I = eye(4,5) for I = eye(4,5) disp('Current unit vector:') disp(I) end I |
s = 10; H = zeros(s); for c = 1:s for r = 1:s H(r,c) = 1/(r+c-1); end end H |
A = nan(20,1); for i=10:-1:-10 if(i <= 0) break; end A(i) = i; end A(:) |
A = nan(20,1); for i=1:20 if(mod(i,2) == 0) continue; else A(i) = i; end end A |
for i = 1:2:20 A(i) = 2; end A |
for i = 1:3 for j = 1:3 A(i,j) = i + j; fprintf('i=%d j=%d -- A(%d,%d)=%d\n',i,j,i,j,A(i,j)); end end A |
for i = 1:3 for j = 1:3 for k = 1:3 A(i,j,k) = i + j + k; fprintf('i=%d j=%d k=%d -- A(%d,%d,%d)=%d\n',i,j,k,i,j,k,A(i,j,k)); end end end A |
tic x = 0; for k = 2:1000000 x(k) = x(k-1) + 5; end toc tic x = zeros(1, 1000000); for k = 2:1000000 x(k) = x(k-1) + 5; end toc Elapsed time is 0.181970 seconds. Elapsed time is 0.011886 seconds. |
tic i = 0; for t = 0:.01:100000 i = i + 1; y(i) = sin(t); end toc tic t = 0:.01:100000; y = sin(t); toc Elapsed time is 2.255200 seconds. Elapsed time is 0.141934 seconds. |
tic i = 0; for t = 0:.01:100000 i = i + 1; y(i) = sin(t); end toc tic i = 0; for t = 0:.01:100000 i = i + 1; y(i) = sin(t); z = linspace(0,2*pi); r = cos(z); end toc Elapsed time is 2.211985 seconds. Elapsed time is 88.842359 seconds. |
x = 0; s = 1/2; upper = 1000; for i = 0:upper x = x + (s)^i; end fprintf('Let x = %.4f\n\n',s); fprintf('sum(x^n/n!) = %.4f\n\n',x); fprintf('where n = 0 to %d\n\n',upper); y = 1/(1-s); fprintf('1/(1-x) = %.4f\n\n',y); |
x = 0; s = 1/2; upper = 1000; for i = 0:upper x = x + (s)^i/factorial(i); end fprintf('Let x = %.4f\n\n',s); fprintf('sum(x^n) = %.4f\n\n',x); fprintf('where n = 0 to %d\n\n',upper); y = exp(s); fprintf('1/(1-x) = %.4f\n\n',y); |
tic hold on b = 0:.00001:2*pi; for i = 0:.01:2*pi x(m) = sin(i); y(m) = cos(i); z(m) = i*sin(i); m = m + 1; end plot(b,x,':b') plot(b,y,':r') plot(b,z,':g') toc |
tic hold on for i = 0:.01:2*pi clear x clear y clear z x = sin(i); y = cos(i); z = i*sin(i); plot(i,x,':b') plot(i,y,':r') plot(i,z,':g') end toc |
tic n = 800; niter = 40; x0 = -2; x1 = 1; y0 = -1.5; y1 = 1.5; [x,y] = meshgrid(linspace(x0, x1, n), linspace(y0, y1, n)); c = x + 1i * y; z = zeros(size(c)); k = zeros(size(c)); for ii = 1:niter z = z.^2 + c; k(abs(z) > 2 & k == 0) = niter - ii; end figure, imagesc(k), colormap jet axis square toc |
"My job is a decision-making job, and as a result, I make a lot of decisions." |
"I tell you that Michael Jackson is unbelievable,
isn’t he? He’s just unbelievable."
|