%CNS120 %matlab tutorial % % %functions and keywords needed for HW5 are: %find, bar, histc/hist, load, subplot, length, xlim/ylim, xlabel, ylabel, %title, for, ttest2 %select lines in editor and press F9 to execute. %---- variables, datastructures cd('/home/urut/caltech/classes/TA/cns120/'); %go to dir where you put all the stuff from the HP %don't need to declare variables, is automatic a=10; b=50; a+b c1=1:100; %make array of length 100, with elements 1...100 c2=1:20:100; %make array of length 5, with elements 1,21,41,61,81 c2 c2(5) % access by index c2([1 2 3]) %access by index c2(1:3) %access by index c2=[c2 55 66 77]; %add elements c2=c2-10; %subtract 10 from each element c3=[c1 c2]; %---- how to make figures randNumbers = randn(1000,1); %1000 random numbers, mean zero, std 1 %mean and standard deviation mean(randNumbers) std(randNumbers) %plot x-y points, connected by lines figure(1) plot( 1:1000, randNumbers ); %plot histograms figure(2) hist( randNumbers, 100); %histogram, 100 bins xlabel('random number'); ylabel('frequency'); title('Histogram xx'); %---- use find and subplot %find all random numbers smaller -1 inds = find( randNumbers < -1 ); randNumbersSmall = randNumbers(inds); %how many numbers were smaller than -1 length(inds) %more complicated find: how many numbers are between -1 and -0.5 ? inds2 = find( randNumbers < -0.5 & randNumbers > -1.0 ); length(inds2) %---- how to make barplots figure(20) bar( 1:2, [length(inds) length(inds2)]); ylabel('# entries'); set(gca,'XTickLabel',{'< -1', '-1