%%% A script to illustrate some simple matlab concepts %%% Creating and fiddling with matrices disp('%% create a matrix of ones'); disp('%% x = ones(3,3)'); x = ones(3,3) disp('%% create a matrix of random values'); disp('%% y = rand(3,3)'); y = rand(3,3) disp('%% Add them'); disp('%% z = x + y'); z = x + y disp('%% concatenate the rows'); disp('%% zz = [x,y]'); zz = [x,y] %% a 3 by 6 matrix disp('%% concatenate the columns'); disp('%% zz = [x;y]'); zz = [x;y] %% a 6 by 3 matrix disp('%% symmetrize y') disp('%% w = y + y'' '); w = y + y' %%% here ' denotes transpose disp('%% the first two rows of the first and 3rd columns'); disp('%% w = w(1:2,1,3)'); w = w(1:2,[1,3]) disp('%% string it out as a vector'); disp('%% wv = w(:) '); wv = w(:) disp('%% essentially dot product'); disp('%% normval = wv'' * wv '); normval = wv' * wv disp('%% same as the above'); disp('%% normval = dot(wv,wv) '); normval = dot(wv,wv) disp('%% count the number of positive elements'); disp('%% sum(wv > 0) '); sum(wv > 0) disp('%% set all the zero values to 1'); disp('%% wv(wv == 0) = 1 '); wv(wv == 0) = 1 disp('%% divide each element of wv by the'); disp('%% corresponding element of the reversed string'); disp('%% wv = wv./wv(end:-1:1) '); wv = wv./wv(end:-1:1) disp('%% Create a string'); str = 'teststring' disp(['a test string is ','string']) %% string concatenation disp(sprintf('with length %d',length(str))) %% formatting %%% Some image things disp('%% read an image'); disp('%% im = imread(''peppers.png''); '); im = imread('peppers.png'); disp('%% % should be uint8'); disp('%% class(im) '); class(im) disp('%% % convert to preferred doubles in range 0 to 1'); disp('%% im = double(im)/255; '); im = double(im)/255; disp('%% show it'); disp('%% imshow(im) '); imshow(im) disp('%% green band'); disp('%% gim = im(1:2:end,1:2:end,2); '); disp('%% red band');gim = im(1:4:end,1:4:end,2); disp('%% rim = im(1:4:end,1:4:end,1); '); disp('%% blue band');rim = im(1:4:end,1:4:end,1); disp('%% bim = im(1:4:end,1:4:end,3); '); bim = im(1:4:end,1:4:end,3); disp('%% show all the separate bands'); newim = [rim;gim;bim]; imshow(newim); imwrite(newim,'newim.pgm','pgm');