1 / 28

1012 MATLAB 教學

1012 MATLAB 教學. 羅英倉 2014/02/25. What is MATLAB?. MAT rix LAB oratory 的 縮寫 1984 年由 MathWorks 公司 推出 主要特色 強大的數值運算 ( numeric computation) 能力 完整的矩陣運算指令 許多 不同領域 的工具箱 (Toolbox ) 、函式庫 影像處理 、訊號 處理、金融分析、生物資訊 應用 algorithm development, data visualization, data analysis, and numeric computation.

Download Presentation

1012 MATLAB 教學

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 1012 MATLAB 教學 羅英倉2014/02/25

  2. What is MATLAB? • MATrixLABoratory的縮寫 • 1984年由MathWorks公司推出 • 主要特色 • 強大的數值運算(numeric computation)能力 • 完整的矩陣運算指令 • 許多不同領域的工具箱(Toolbox)、函式庫 • 影像處理、訊號處理、金融分析、生物資訊 • 應用 • algorithm development, data visualization, data analysis, and numeric computation

  3. Development Environment • 4個主要視窗 • Workspace(變數區域)、Command History(命令歷史列)、Command History、Current Folder

  4. Operators • 算術運算子 • 沒有++、-- • +、-、*、/(除以)、\(除)、^(次方) • >> 100/5ans= 20 • 比較運算子 • >=、<=、>、<、==、~= • 邏輯運算子 • &、|、~ • Element-by-Element運算“.” A = [1 1 1;1 2 3;1 3 6]; A = 1 1 1 1 2 3 1 3 6 A.^2 = 1 1 1 1 4 9 1 9 36 A^2 = 3 6 10 6 14 25 10 25 46

  5. Operators • 運算式最後加分號(;),就不會顯示運算結果 • >> 100/5;%不會輸出運算結果 • 多個運算式之間,可用逗號(,)或分號(;)隔開 • x=1,y=2,z=3 • x=1;y=2;z=3 • %註解符號,類似C語言的// • 快捷鍵:Ctrl+R(註解) 、Ctrl+T(解除註解)

  6. Variables(1/3) • 區分大小寫(case sensitive) • 變數第一個字母必需是英文字母 • Ex: num_students= 30 • 不需要預先宣告變數 • 輸入變數名稱,會顯示變數值 • 預設變數 • ans: Most recent answer • pi: 3.1416 • i or j: 虛數基本單位,也就是根號負1 • Inf:無窮大(Infinity) • NaN:不存在 • realmin:可以使用的最小實數 • realmax:可以使用的最大實數

  7. Matrices and Arrays • 矩陣index從1開始算 • 產生一個 1x3 的矩陣,可用空格或逗號隔開 • x=[10 2 3] 或 y=[1,2,3] • 產生一個 2x3 的矩陣,分號作為換列的分隔符號 • m=[11 12 13; 21 22 23] • 直接存取該位置的值 • x(2)=2 • m(2,3)=23 • 亦可由變數組成 • z=[x y] % z=[10 2 3 1 2 3] • 刪除元素 • z(2)=[] % z=[10 3 1 2 3] • 增加元素 • z(6)=100 % z=[10 3 1 2 3 100]

  8. Matrices and Arrays • 矩陣列跟欄的操作 • x=[1 2 3; 4 5 6] • x(2,:) % 顯示第二列資料,其結果 4 5 6 • x(:,3) % 顯示第三欄資料,其結果 3 6 • x(2,1:2)=7 % x=[1 2 3; 77 6] • x(1,end) %ans = 3 • 從哪裡到哪裡 • a=1:3 % 從 1~3 每次間隔 1 • b=1:.5:3 % 從 1~3 每次間隔 0.5 • c=linspace(0,10,20) % 從 0~10 分成 20 等分 • %linspace(Generate linearly spaced vectors) • x(2:6)=1 % 將 x 向量的第二到六個設定 1

  9. 矩陣基本運算 • x = [2 2 2];y = [3 3 3];z = [1 1 1];x+y-z = [4 4 4] • 矩陣相乘A = [5 3 2 6];A = 5 3 2 6B = [-4 9 0 1]’B = -4 9 0 1A*B = 13 • 如果要element by element 的相乘,應該用”.*”,而不是”*”

  10. 矩陣常用函式 • zeros: 配置零矩陣 • zeros(3)%產生一個3-by-3的零矩陣 • ones: 配置每個element都是1的矩陣 • eye: 配置單位矩陣 • eye(3) • rand, randi, randn: 配置值為亂數的矩陣 • size:取得矩陣的大小 • length: 取得陣列的長度 • inv: 取得反矩陣 • sort: 排序 • magic(n): 產生n x n的魔方陣(每行、每列、和對角線的總和都一樣)

  11. 數學常用函式 • min、median、max • mean: 平均 • sum: 總和 • abs: 取絕對值 • ceil、floor、round(四捨五入) • sin、cos、tan、asin、acos、atan • exp: 自然指數 • log(x):自然對數 ln(x), log2, log10 • sqrt(開根號) • rem(x,y):x 除以 y 的餘數 • sign: 回傳-1(負數), 0(零) or 1(正數) • imag(取虛數部份)、real(取實數部份)

  12. 一般常用函式 • clear: 清除所有變數宣告 • 亦可僅清除單一變數 clear x; • clc: 清除Command Window畫面 • disp: 顯示文字或陣列

  13. 訊號系統常用函式 • conv(a,b):Convolution • fft2(x):Fast Fourier transform

  14. Custom M-file • 副檔名: *.m • File->New->M-File or File->New->Script • 檔名必須與函數名稱相同 • M-file函式裡面定義的任何變數皆是 local variable • 一般習慣將指令先寫個M-File,然後執行該 M-File(等同於在Command Window下指令),以便多次執行相同指令 • Function 格式 • function [ output_args ] = FuncName( input_args )% UNTITLED Summary of this function goes here% Detailed explanation goes herecodereturn

  15. M-file Example function [z] = add(x,y) z=x+y; return • 在Command Window可以直接呼叫add() • add(4,80) • Current Folder 要設定在M-file所在位置,不然會找不到該函式,發生”??? Undefined function or method”的錯誤

  16. if/elseif/else • 與 C 語言相同,看情況使用 elseif跟 else • if expression1 commandselseif expression2 commandselseif … … …else commandsend

  17. For Loop • for i=1:10 % i 從 1~10 間隔 1 x(i)=sin(i)end • fori=1:2:10 % i從 1~10 間隔 2x(i)=cos(i)end • fori=10:-1:1 % i從 10~1 每次少 1 x(i)=tan(i)end • for迴圈方法較慢,可直接向量操作 • i=1:10;x=sin(i)

  18. while loop • while • i=0;whilei<10i = i+1end • 迴圈可使用 break,continue

  19. Graphics (1/4) • plot:畫2D線圖 • 樣式設定 • 顏色 • y(yellow)、m(magenta)、c(cyan)、r(red)、green(g)、b(blue)、white(w)、black(b) • 線條樣式 • .(point)、o(circle)、x(x-mark)、+(plus)、*(star)、-(solid line)、:(dotted line)、--(dashed) x=linspace(1,2*pi,30); y=sin(x); plot(x,y); z=cos(x); plot(x,y,x,z); plot(x,y,'o',x,z,'g*');

  20. Graphics (2/4) • stem(x,y): 繪製離散圖形 • grid: 格線 • xlabel('x name'): x 軸名稱 • ylabel('y name'): y 軸名稱 • title('stem(x,sin(x))'): 圖片抬頭 • text(0,0,'text'): 特定位置加上文字 • axis([xminxmaxyminymax]): x、y 軸的範圍 x=linspace(1,2*pi,30); y=sin(x); stem(x,sin(x)); gridon; xlabel('x name'); ylabel('y name'); title('Test Title');

  21. Graphics (3/4) • subplot(共幾列,共幾欄,第幾個位置)%選擇輸出的位置 • 分好幾個小圖顯示 • Example x=linspace(0,2*pi,30); y=sin(x); z=cos(x); a=2*sin(x).*cos(x); b=tan(x); subplot(2,2,1); plot(x,y); subplot(2,2,2); plot(x,z); subplot(2,2,3); plot(x,a); subplot(2,2,4); plot(x,b);

  22. Graphics (4/4) • hold on / hold off • 在已有的圖案上,加上新的圖案 • % plot(x,z) 的結果會覆蓋 plot(x,y) 的結果 • % 在 plot(x,z) 之前加上 hold on,保留之前畫得結果 • x=linspace(1,2*pi,30); • y=sin(x); • plot(x,y); • z=cos(x); • plot(x,z); • x=linspace(1,2*pi,30); • y=sin(x); • plot(x,y); • z=cos(x); • hold on • plot(x,z); • hold off

  23. Input/Output • fprintf • print to the screen x= 100; fprintf('hello world! %d\n',x); • 寫檔 x = 0:.1:1; y = [x; exp(x)]; % open the file with write permission fid = fopen('c:\exp.txt', 'w'); fprintf(fid, '%6.2f %12.8f\n', y); fclose(fid); % view the contents of the file type exp.txt • input:select=input('selection(number):');

  24. 音效 (1/3) • 讀取音效 • [y,fs]=wavread('file.wav'); • 可設定讀取範圍,如 wavread('file.wav',[3000,5000]); • 播放音效 • sound(y,fs); • wavplay(y*m,fs*n); • y 增加倍數(m)可增大音量,fs增加倍數(n)會影響音訊速度 • 錄製音效 • y=wavrecord(n, fs); %讀入 n 點資料 • 輸出檔案 • wavwrite(y, fs, nbits, waveFile)

  25. 音效 (2/3) • wavread範例 • wavrecord範例 [y, fs]=wavread('run.wav'); sound(y, fs); % 播放此音訊 time=(1:length(y))/fs; % 時間軸的向量 plot(time, y); % 畫出時間軸上的波形 fs=12000;%取樣頻率 12000 Hz fprintf('按任意鍵後開始 2秒錄音:'); pause fprintf('錄音中...'); y=wavrecord(2*fs, fs); %duration*fs是錄音資料點數 fprintf('錄音結束\n'); fprintf('按任意鍵後開始播放:'); pause wavplay(y,fs);

  26. 音效 (3/3) • 音效Convolution範例 file='sentence.wav'; [y,fs,nbits]=wavread(file); file2='Hallway_IR.wav'; [y2,fs2,nbits2]=wavread(file2); test=conv(y,y2); %迴旋積運算 wavwrite(test,fs2,'test.wav'); time=(1:length(test))/fs; %時間軸 plot(time,test);

  27. 影像處理範例 • imread(‘filename.jpg'): 讀取影像檔案 • Example: filename = 'dog.jpg'; rgb_image= imread(filename); figure,imshow(rgb_image); %彩色轉成灰階 gray_image = rgb2gray(rgb_image); figure,imshow(gray_image); imwrite(gray_image, 'gray_dog.jpg'); %write new image %影像模糊化 h = fspecial('gaussian',10,10); blur_rgb_image=imfilter(rgb_image,h); figure,imshow(blur_rgb_image);

  28. 影像處理範例 • 灰階影像的邊界 edge_img= edge(gray_image,'canny'); imshow(edge_img);

More Related