Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB Basic. Basic Data Elements The basic elements that MATLAB uses are matrices To form a matrix: Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15.

Similar presentations


Presentation on theme: "MATLAB Basic. Basic Data Elements The basic elements that MATLAB uses are matrices To form a matrix: Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15."— Presentation transcript:

1 MATLAB Basic

2 Basic Data Elements The basic elements that MATLAB uses are matrices To form a matrix: Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15 16 ] Show : A = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

3 Basic Data Elements (cont.) Edit the element in the matrix: * Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15 16 ] ; A(1,3)=0 ; A Show : A = 1 2 0 4 5 6 7 8 9 10 11 12 13 14 15 16

4 Basic Data Elements (cont.) Generate row vectors of equally spaced : * Example 1: Enter : x=2:6 Show : x = 2 3 4 5 6 * Example 2: Enter : x=1.2:0.2:2 Show : x = 1.2000 1.4000 1.6000 1.8000 2.0000

5 Submatrices Create a submatrix of the matrix A entered earlier, use the : to specify the rows and columns Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15 16 ]; C=A(2:3,2:4) Show : C = 6 7 8 10 11 12

6 Submatrices (cont.) If the colon is used by itself for one of the arguments, either all the rows or all the columns of the matrix will be included * Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15 16 ]; A(:,2:3) Show : ans = 2 3 6 7 10 11 14 15 * Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15 16 ]; A(4,:) Show : ans = 13 14 15 16

7 Submatrices (cont.) Generate a submatrix using nonadjacent rows or columns by using vector arguments to specify which rows and columns are to be included *Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15 16 ]; E=A([1,3],[2,4]) Show : E = 2 4 10 12

8 Generating Matrices Generate matrices using built-in MATLAB functions *Enter : B=rand(4) Show : B =0.8180 0.3412 0.8385 0.5466 0.6602 0.5341 0.5681 0.4449 0.3420 0.7271 0.3704 0.6946 0.2897 0.3093 0.7027 0.6213 To build triangular or diagonal matrices, we can use the MATLAB functions triu, tril and diag

9 Matrices Arithmetic We multiply matrix A times B by typing A*B The sum and difference of A and B are given by A+B and A-B, respectively The transpose of A is given by A’ The matrix is computed by typing A^5 * Enter : W=[1 2 ; 3 4 ]; X=W^2 Y=W.^2 Show : X = 7 10 Y = 1 4 15 22 9 16

10 Programming Features The line beginning with a % are comments that are not executed MATLAB has six relational operators that are used for comparisons of scalars or elementwise comparisons of arrays (1) < : less than (2)<= : less than or equal (3) > : greater than (4)>= : greater than or equal (5)==: equal (6)~= : not equal

11 Programming Features (cont.) 3 logical operators: (1) & : AND (2) | : OR (3) ~ : NOT Enter : A=[1 0 1 ; 0 1 1 ; 0 0 1 ]; B=[-1 2 0 ; 1 0 3 ; 0 1 2 ]; X=A&B Y=A|B Z=~A Show : X = 1 0 0 Y =1 1 1 Z = 0 1 0 0 0 1 1 1 1 1 0 0 0 0 1 0 1 1 1 1 0

12 Columnwise Array Operator MATLAB has a number of functions that, when applied to either a row or column vector x, return a single number When used with a matrix argument, these functions are applied to each column vector and the results are returned as a row vector

13 Columnwise Array Operator (cont.) Enter : A=[-3 2 5 4 ; 1 3 8 0 ; -6 3 1 3] ; m=min(A) M=max(A) S=sum(A) product=prod(A) Show: m = -6 2 1 0 M = 1 3 8 4 S = -8 8 14 7 product = 18 18 40 0

14 Graphics Plot the function on the interval [0,10] Enter : x=0:0.2:10 y=sin(x)./(x+1) plot(x,y)

15 Graphics (cont.) Plot the function on the interval [0,10] and compare to the graph z=sin(x) Enter : x=0:0.2:10 y=sin(x)./(x+1) z=sin(x) plot(x,y,x,z)

16 Help Facility Click on the help button in the toolbar or type helpbrowser in the command window Provides help on all MATLAB features Lists and describes all the MATLAB functions, operations and commands

17 Website http://neural.cs.nthu.edu.tw/jang/ ( 張智星 ) http://neural.cs.nthu.edu.tw/jang/ http://www.mathworks.com/ http://cwww.ee.nctu.edu.tw/course/LA2005/index.htm (Slides) http://cwww.ee.nctu.edu.tw/course/LA2005/index.htm


Download ppt "MATLAB Basic. Basic Data Elements The basic elements that MATLAB uses are matrices To form a matrix: Enter : A=[1 2 3 4; 5 6 7 8;9 10 11 12; 13 14 15."

Similar presentations


Ads by Google