echo off clear all; close all; home echo on %to run matlab: execute "matlab" on linux.andrew.cmu.edu pause; clc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Getting around %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %for help topics: "help" pause; help pause; %help on specific topic lists available commands: "help " pause; help strfun pause %help on the command: "help " pause; help hex2num pause %help on help: "help help" pause; help help pause %to see available matlab demos: help demos pause; help demos pause; %to clear screen: "clc" pause; clc pause; %working with directories: ls, pwd, cd ... pause; ls pause; %to execute shell command: !command pause; !mem pause; clc; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Working with variables %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %all variables are vectors and matrices %creating x as a row vector x = [1 2 3 5] pause; %creating x as a column vector x = [1; 2; 3; 5] pause; %creating x as a column vector using transpose operator x = [1 2 3 5]' pause; %not to print the result of operation: put ; at the end y = [1; 2; 3; 5]; pause; clc; %to see all the variables who pause; %to print the value of any variable: type the name of the variable x pause; %to access individual elements (matlab uses 1-based index) x(2) pause; %to access a range of elements x(2:4) pause; clc %to save ALL variables into a file save mydata.mat; ls pause; %to clear a variable from the memory clear y; who pause; %to clear all variables from the memory clear; who pause; %to load a file with variables load mydata.mat; who pause; %to save specific variables save -ascii x.mat x; ls pause; !more x.mat pause; clc; %to load a delimited file (relevant for hw2) y = dlmread('x.mat', ' ') pause; clc; %% to create a matrix B = [1 2 3; 4 5 6] pause; %accessing elements of B %the order is RC (row column) B(1,2) pause; %accessing rows/columns B(:,1) pause; %accessing ranges of elements B(2,1:2) pause; %accessing a set of specific elements ind = [1 3]; B(2,ind) pause; %overwriting elements B(2,1:2) = [8 9] pause; clc; %creating vectors using [from:incr:to] x = [1:0.1:2] y = [5:-1:0] pause; %creating vectors using zeros and ones x = zeros(2,2) y = 3*ones(3,2) pause; %size of a matrix size(y) x = ones(size(y)) pause; clc; % maximum and minimum for each column x = [1:1:4; 7:1:10] max(x) min(x) max(max(x)) pause; % summations for each column sum(x) sum(sum(x)) pause; clc; %identity matrix E = eye(2,2) pause; clc; % basic operations for element by element: use "." A = [1 2; 4 5] B = [2 2; 3 3] C = A.*B pause; C = C./B pause; C = 3*A pause; C = A/3 pause; %power C = A.^3 pause; %power C = exp(A) pause; clc; % basic operations on matrices A = [1 2; 4 5] B = [2 2; 3 3] C = A+B pause; C = A*B pause; %inverse of A C = inv(A) pause; %transpose of A C = A' pause; clc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Strings %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %strings are treated as arrays s1 = 'hello' s1(1:4) pause; %to print use fprintf(FILEID, string with format, variables); %FILEID = 1 -> on screen fprintf(1, 's = %s\n', s1); pause; clc; %to print into file fid1 = fopen('temp.txt', 'w'); fprintf(fid1, 'A(1,2) = %d \n', A(1,2)); fclose(fid1); !more temp.txt pause; clc; %for more help with strings: help strfun clc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Few statistics commands %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %generating uniform iid values from 0 to 1 A = rand(2) pause; %generating normal iid values from N(0,1) A = randn(1,10) pause; %reset the seeds for rand and randn generators rand('state',sum(100*clock)); randn('state',sum(100*clock)); pause; clc; %PDFs and CDFs: help pdf and help cdf for supported distributions x = [-2:0.25:2]; f = pdf('Normal',x,0,1) pause; F = cdf('Normal',x,0,1) pause; f = pdf('Uniform',x,-2,2) pause; F = cdf('Uniform',x,-2,2) pause; clc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% Plotting %% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %use plot function x = [-2:0.25:2]; y = pdf('Normal',x,0,1); plot(x,y); pause; clc; %to open or switch to another figure: figure(x) figure(2); y = pdf('Uniform',x,-2,2); plot(x,y); pause; clc; %put a grid on top of the window figure(1); grid; figure(2); grid; pause; clc; %plotting on top of existing graph will override unless "hold on" is set figure(1); hold on; y = cdf('Normal',x,0,1); plot(x,y, '*'); figure(2); hold on; y = cdf('Uniform',x,-2,2); plot(x,y, '*'); pause; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %% For the rest of useful things in matlab %% see matlab help (concise and helpful), %% matlab tutorials on the web, matlab demos %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% echo off