初学MATLAB对程序解析.doc_第1页
初学MATLAB对程序解析.doc_第2页
初学MATLAB对程序解析.doc_第3页
初学MATLAB对程序解析.doc_第4页
初学MATLAB对程序解析.doc_第5页
已阅读5页,还剩4页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

% Introducing Matlab % adapted from Eero Simoncelli and S.F. Chang% additions made by Xin Li, Jan. 2005-2009% % (1) Help and basics % The symbol % is used in front of a comment. % The current directory is displayed at the top-right corner of MATLAB window% To change the current directory, click the . botton or use cd if% you like % Use dir to see the list files in current directory % To get help type help (will give list of help topics) or help topic % If you dont know the exact name of the topic or command you are looking for,% type lookfor keyword (e.g., lookfor regression)% Note: I suggest you to use MATHWORKS online help or Google instead % When writing a long matlab statement that exceeds a single row use .% to continue statement to next row. % When using the command line, a ; at the end means matlab will not% display the result. If ; is omitted(省略) then matlab will display result. % Use the up-arrow to recall commands without retyping them (and down% arrow to go forward in commands). % Other commands borrowed from emacs and/or tcsh:% C-a moves to beginning of line (C-e for end), C-f moves forward a% character (C-b moves back), C-d deletes a character, C-k deletes % the line to the right of the cursor, C-p goes back through the% command history and C-n goes forward (equivalent to up and down arrows). % (2) Objects in matlab - the basic objects in matlab are scalars,% vectors, and matrices. N = 5 % a scalar,一个数量v = 1 0 0 % a row vector,一行变量v = 1;2;3 % a column vector,一列变量v = v % transpose a vector (row to column or column to row)行列转置v = 1:.5:3 % a vector in a specified range:定义一个变化的变量,规定了范围和变化的尺度 v = pi*-4:4/4 % start:stepsize:endv = % empty vector m = 1 2 3; 4 5 6 % a matrix: 1ST parameter is ROWS % 2ND parameter is COLS ,2行3列m = zeros(2,3) % a matrix of zerosv = ones(1,3) % a matrix of onesm = eye(3) % identity matrix,3阶单位阵v = rand(3,1) % rand matrix (see also randn),3行1列的随机数save matrix_data m % save the variable m to a file named matrix_data.mat clear all % clear all variables currently used by MATLAB load matrix_data % read data from the saved file m % display it - it is still there! v = 1 2 3; % access a vector elementlength(v) % length of a vectorv(3) % vector(number),v中的第三个元素 m = 1 2 3; 4 5 6m(1,3) % access a matrix element,第1行第3列的元素 % matrix(rownumber, columnnumber)m(2,:) % access a matrix row (2nd row),第2行的所有元素m(:,1) % access a matrix column (1st row),第一列的所有元素 size(m) % size of a matrix,矩阵的大小,2行3列size(m,1) % number rows,求出行数size(m,2) % number of columns,求出列数 m1 = zeros(size(m) % create a new matrix with size of m,使得m中的所有元素全变为0,得到一个新的矩阵。 who % list of variables,可以选择查看所使用的各个量的情况whos % list/size/type of variables,显示所有量的情况 % (3) Simple operations on vectors and matrices % (A) Pointwise (element by element) Operations: % addition of vectors/matrices and multiplication by a scalar% are done element by elementa = 1 2 3 4; % vector2 * a % scalar multiplicationa / 4 % scalar multiplicationb = 5 6 7 8; % vectora + b % pointwise vector additiona - b % pointwise vector additiona . 2 % pointise vector squaring (note .)a .* b % pointwise vector multiply (note .)a ./ b % pointwise vector multiply (note .) log( 1 2 3 4 ) % pointwise arithmetic operation,使用的是e为底round( 1.5 2; 2.2 3.1 ) % pointwise arithmetic operation % (B) Vector Operations (no for loops needed)% Built-in matlab functions operate on vectors, if a matrix is given,% then the function operates on each column of the matrix a = 1 4 6 3 % vectorsum(a) % sum of vector elementsmean(a) % mean of vector elementsvar(a) % variance (sigma2),方差的计算std(a) % standard deviation (sigma),标准差的计算max(a) % maximum a = 1 2 3; 4 5 6 % matrixmean(a) % mean of each columnmax(a) % max of each column max(max(a) % to obtain max of matrix max(a(:) % another way to obtain max of matrix % (C) Matrix Operations: 1 2 3 * 4 5 6 % row vector 1x3 times column vector 3x1 % results in single number, also % known as dot product or inner product,注意一撇代表了转置 1 2 3 * 4 5 6 % column vector 3x1 times row vector 1x3 % results in 3x3 matrix, also % known as outer product a = rand(3,2) % 3x2 matrixb = rand(2,4) % 2x4 matrixc = a * b % 3x4 matrix a = 1 2; 3 4; 5 6 % 3 x 2 matrixb = 5 6 7; % 3 x 1 vectorb * a % matrix multiplya * b % matrix multiply % (D) 1D linear convolution:a = 1,2,3,4; % input signal I b = 1,-1; % input signal II conv(a,b) % convolution of two signals %(4) Saving your work save mysession % creates session.mat with all variablessave mysession a b % save only variables a and b clear a b % clear variables a and bclear all % clear all variables load mysession % load sessionab %(5) Relations and control statements % Example: given a vector v, create a new vector with values equal to% v if they are greater than 0, and equal to 0 if they less than or% equal to 0. v = 3 5 -2 5 -1 0 % 1: FOR LOOPSu = zeros( size(v) ); % initializefor i = 1:size(v,2) % number of columns,求出列数 if( v(i) 0 ) u(i) = v(i); endendu v = 3 5 -2 5 -1 0 % 2: NO FOR LOOPSu2 = zeros( size(v) ); % initializeind = find( v0 ) % index into 0 elements u2(ind) = v( ind ) % other control commands % while, switch-case, etc% Tip 1: try to avoid the use of loops because MATLAB has excellent support of% vector(matrix)-based operations. You will be amazed by how many things% can be done without loops. %(6) Creating functions using m-files:% Functions in matlab are written in m-files. Create a file called % thres.m In this file put the following: % you can write you own variance calculation functionfunction v=var_mine(x)N=length(x); % get the lengthm=sum(x)/N; % obtain the mean, you can also use m=mean(x);v=sum(x.2)/N-m*m; % calculate the variance% cut and paste Lines 208-211 into a new file and save it as var_mine.m% now you can test your own function vs. the one offered by MATLABvar(x)var_mine(x) % function can have multiple inputs and outputs% Example 1: a function that packs two matrices A,B into a larger matrice% C=A,B;B,Afunction C=matrice_packing(A,B)C=A,B;B,A;% Example 2: calculate both mean and variance of a vectorfunction m,v=mean_and_var(x)m=mean(x);v=var(x); % !Cautious Notes!: if the name of your own MATLAB file is the same as some% function name used by MATLAB itself, MATLAB will use the one with a% higher priority defined in the PATH setting.% Tip 2: When to write a new function? A thumb rule is when you need to% call this function again and again (the same rule as in C programming). %(7) Plotting x = 0 1 2 3 4; % basic plottingplot( x );plot( x, 2*x ); %建立了横纵坐标的关系axis( 0 8 0 8 ); %规定了横纵坐标的范围 x = pi*-24:24/24;plot( x, sin(x) );xlabel( radians );ylabel( sin value );title( dummy );gtext( put cursor where you want text and press mouse ); figure; % multiple functions in separate graphs % figure command creates a new figure windowsubplot( 1,2,1 ); % creates 1x2 subgraphs, window 1 as current graph,对平面的分割显示图像plot( x, sin(x) );axis square;subplot( 1,2,2 );plot( x, 2.*cos(x) );axis square; figure; % multiple functions in single graphplot( x,sin(x) );hold on; plot (x, 2.*cos(x), - );legend( sin, cos );hold off; %在一幅图像中显示两张图 figure; % matrices as imagesm = rand(64,64);imagesc(m) % Scale data and display as imagecolormap gray;axis imageaxis off; n = zeros(64); % generate a square imagen(17:48,17:48)=1;imshow(n,); % use help imshow to find out what means subplot(1,2,1), subimage(m) % display two images in parallelsubplot(1,2,2), subimage(n)% Tip 3: showing your results visually often helps the debugging of your% MATLAB programs. %(8) Image I/O operations % cameraman.tif is a test image provided by MATLAB% if you want to read your own image into MATLAB, make sure that the image% is under a directory recognized by MATLAB path settingx=imread(cameraman.tif); % read in the imagewhos x % display information about ximshow(x,); % display the imagex(1:8,1:8) % elements in x are unsigned integers y=fliplr(x); %flip image left to rightimshow(y,)imwrite(y,camera_flip.bmp); % write the flipped image out as a BMP file % if you want to apply arithmetic operations on uint8 type of data,% you need to convert them into double firstz=(double(x)+double(y)/2; % linearly mixed two im

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论