Matlab Introduction for 33IRO ----------------------------- Run Matlab and type in the followong commands tha apear after ">>" prompt: >> help sum SUM Sum of elements. For vectors, SUM(X) is the sum of the elements of X. For matrices, SUM(X) is a row vector with the sum over each column. For N-D arrays, SUM(X) operates along the first non-singleton dimension. SUM(X,DIM) sums along the dimension DIM. Example: If X = [0 1 2 3 4 5] then sum(X,1) is [3 5 7] and sum(X,2) is [ 3 12]; See also PROD, CUMSUM, DIFF. Overloaded methods help sym/sum.m >> helpdesk >> z = zeros(20,20); >> o = ones(5,5); >> size(o) ans = 5 5 >> size(z) ans = 20 20 >> whos Name Size Bytes Class ans 1x2 16 double array o 5x5 200 double array z 20x20 3200 double array Grand total is 427 elements using 3416 bytes >> z(5:9,5:9)=o; >> x=[1:5;2:6] x = 1 2 3 4 5 2 3 4 5 6 >> sum(x) ans = 3 5 7 9 11 >> x(:) ans = 1 2 2 3 3 4 4 5 5 6 >> sum(x(:)) ans = 35 >> x(x>3)=0 x = 1 2 3 0 0 2 3 0 0 0 >> x([1,2,3,4]) ans = 1 2 2 3 >> x = [1:1000000]; tic for i = 1:1000000 if(x(i)>30) x(i)=0; end end toc elapsed_time = 14.1008 >> tic x(x>30)=1; toc elapsed_time = 4.9092 >> x = [1:5;2:6] x = 1 2 3 4 5 2 3 4 5 6 >> max(x) ans = 2 3 4 5 6 >> max(x(:)) ans = 6 >> I = find(x>3) I = 6 7 8 9 10 >> [I,J] = find(x>3) I = 2 1 2 1 2 J = 3 4 4 5 5 ___________________________________________________________ Part 1 >> b = linspace(1,10,10) b = 1 2 3 4 5 6 7 8 9 10 >> a=b(1:2:end) a = 1 3 5 7 9 >> X = [0 0 1 1 1 0 1 0; 1 0 0 1 0 0 1 1; 0 1 1 1 1 0 0 1; 1 1 1 0 0 0 1 0] X = 0 0 1 1 1 0 1 0 1 0 0 1 0 0 1 1 0 1 1 1 1 0 0 1 1 1 1 0 0 0 1 0 >> sum(X') ans = 4 4 5 4 >> max(sum(X')) ans = 5 x = Columns 1 through 7 0.8180 0.6602 0.3420 0.2897 0.3412 0.5341 0.7271 Columns 8 through 10 0.3093 0.8385 0.5681 >> plot(x) >> hold on >> plot(x,'.'); >> grid on >> imshow( X/max(X(:)) ); >> axis on >> XX = zeros(4*size(X)); >> XX(1:4:end,1:4:end) = X; >> imshow( XX/max(XX(:)) ); _______________________________________________________ Part 2 >> image >> im = get(get(gca, 'children'), 'cdata' ); >> max(im(:)) ans = 31.2923 >> min(im(:)) ans = 3.6123 >> im = im/max(im(:))*255; >> max(im(:)) ans = 255 >> min(im(:)) ans = 29.4367 >> imshow( im/max(im(:)) ) >> imwrite(im, [0:255]'*[1 1 1], 'image.bmp', 'bmp' ); >> Im = imread( 'image.bmp' ); >> imshow(Im) >> im = Im(10:40, 10:40); >> imshow(im) >> imshow(Im) >> mesh(double(Im)) >> [x, y] = gradient(double(Im)); >> mesh( sqrt(x.^2+y.^2) ) >> figure >> imagesc( sqrt(x.^2+y.^2) ) >> close all _______________________________________________________________ Part 3 >> x = [1 2 3]' x = 1 2 3 >> M = [1 2 3; 4 5 6; 7 8 9] M = 1 2 3 4 5 6 7 8 9 >> M*x ans = 14 32 50 >> x'*M ans = 30 36 42 >> x'*x ans = 14 >> x*x' ans = 1 2 3 2 4 6 3 6 9 >> cross(x,y) ans = -4 8 -4 >> dot(x,y) ans = 38 >> y'*x ans = 38 >> A = eye(3); >> A A = 1 0 0 0 1 0 0 0 1 >> rank(A) ans = 3 >> A(3,3) = 0; >> a a = 1 3 5 7 9 >> A A = 1 0 0 0 1 0 0 0 0 >> rank(A) ans = 2 >> A(3,3) = eps; >> A A = 1.0000 0 0 0 1.0000 0 0 0 0.0000 >> rank(A) ans = 2 >> A(3,3) = eps*10 A = 1.0000 0 0 0 1.0000 0 0 0 0.0000 >> rank(A) ans = 3 >> A = rand(3,3); >> [v, e] = eig( A * A' ); >> v v = 0.5432 -0.6748 0.4996 -0.7027 -0.0397 0.7104 0.4596 0.7370 0.4957 >> e e = 0.0032 0 0 0 0.0683 0 0 0 3.7450 >> v'*v ans = 1.0000 0.0000 0.0000 0.0000 1.0000 -0.0000 0.0000 -0.0000 1.0000 >> x = [1 2]' x = 1 2 >> A = [1 2; 0 1]; >> b = [3 4]'; >> y = A * x + b y = 8 6 >> y = [A,b]*[x;1] y = 8 6 >> >> >> >> diary off _______________________________________________________________ Part 4 >> A = [5 7 8; 0 1 9; 4 3 6]; A(:,:,2) = [1 0 4; 3 5 6; 9 8 7] A(:,:,1) = 5 7 8 0 1 9 4 3 6 A(:,:,2) = 1 0 4 3 5 6 9 8 7 >> B = cat( 3, [2 8; 0 5], [1 3; 7 9], [2 3; 4 6]) B(:,:,1) = 2 8 0 5 B(:,:,2) = 1 3 7 9 B(:,:,3) = 2 3 4 6 >> C = {1, 'hruska', [1 2; 3 4]} C = [1] 'hruska' [2x2 double] >> C{1} ans = 1 >> C{3} ans = 1 2 3 4 >> C{[1 2]} ans = 1 ans = hruska >> C{:} ans = 1 ans = hruska ans = 1 2 3 4 >> D = {[1 2; 3 4], [5 6]} D = [2x2 double] [1x2 double] >> D{:} ans = 1 2 3 4 ans = 5 6 >> cat(1,D{:}) ans = 1 2 3 4 5 6 _______________________________________________________________ Part 4 Create script file scr.m containing clear all close all whos x = rand(1,10); plot(x,'.-') Create function file fcn.m containing function f = fcn(x) f = plot(x,'.-'); title('plot of x'); xlabel('x axis'); ylabel('y axis'); return >> x = rand(1,100); >> f = fcn(x); >> delete(f);