티스토리 뷰

DIP/Matlab

[Ch2] Basic use of MATLAB

jeong_reneer 2022. 1. 28. 15:08

2.1 Introduction

1) MATLAB

: Data analysis and visualization tool designed with powerful support for matrices and matrix operations

 

2) MATLAB's standard data type : Matrix

- All data are considered to be matrices of some sort

- Images are matrices whose elements are the grey values (or possibly the RGB values) of its pixels

- Single values are considered to be 1x1 matrices, while a string is a 1xn matrix of char (n : string's length)

 

3) MATLAB's function

: A keyword which accepts various parameters, and produces some sort of output (matrix, string, graph, figure)

 

 

2.2 Basic use of Matlab

1) Command line driven : all commands are entered by typing them after the prompt symbol (>>)

2) format function

- Matlab 계산은 내부적으로 double precision BUT default display format은 8 decimal places

- format function 이용해서 display format 바꿀 수 있음

>> format long
>> 11/7

ans =
	1.57142857142857

 

 

2.3 Variables and the workspace

1) Variables : Names to store values

2) Workspace : listing all currently defined variables, their numeric data type and sizes in byte

① [View] - [Workspace]

② whos function : ans is variable (to store result of last calculation)

>> whos

Name 	Size 	Bytes 	Class
a 		1x1 	8 double array
ans 	1x1 	8 double array
b 		1x1 	8 double array
Grand total is 3 elements using 24 bytes

cf) who function : ans is a list of variable names only

>> who

Your variables are:
a ans b

 

 

2.4 Dealing with matrices

>> a=[4 -2 -4 7;1 5 -3 2;6 -8 -5 -6;-7 3 0 1]

 

2.4.1 Matrix element

1) (row, column) Indexing

>> a(2,3)
ans =
    -3

 

2) A single number Indexing

>> a(10)
ans =
    -3
>> a([1 6 11 16])
ans =
    4 5 -5 1

 

3) Colon operator ( : )

→ A vector of values

a:b      All integers from integer a to b

a:i:b   All values from a by increment i up to b

a(:)     All the matrix elements as a single column (세로로 한줄)

>> 2:3:16
ans =
    2 5 8 11 14
    
>> a(2,1:3)
ans =
    1  5  -3
    
>> a(2:4,3)
ans =
    -3
    -5
     0
     
>> a(2:3,3:4)
ans =
    -3   2
    -5  -6

>> a(3,:)
ans =
    6 -8 -5 -6
    
>> a(:,2)
ans =
    -2
     5
    -8
     3

 

2.4.2 Matrix operations

1) Standard Arithmetic operations

: add(+), subtract(-), multiply(*), invert(inv(a)), power(^), transpose(')

 

2) Geometric operations

(1) flipud, fliplr : flipping a matrix up/down and left/right respectively

>> flipud(a)
ans =
   -7    3    0    1
    6   -8   -5   -6
    1    5   -3    2
    4   -2   -4    7
    
>> fliplr(a)
ans =
    7   -4   -2    4
    2   -3    5    1
   -6   -5   -8    6
    1    0    3   -7

 

(2) rot90 : rotating a matrix by 90 degrees

>> rot90(a)
ans =
    7    2   -6    1
   -4   -3   -5    0
   -2    5   -8    3
    4    1    6   -7

 

(3) reshape : producing a matrix with elements taken column by column from the given matrix

>> c=[1 2 3 4 5; 6 7 8 9 10; 11 12 13 14 15; 16 17 18 19 20]
c =
    1    2    3    4    5
    6    7    8    9   10
   11   12   13   14   15
   16   17   18   19   20
    
>> reshape(c,2,10)
ans =
    1  11  2  12  3  13  4  14   5  15
    6  16  7  17  8  18  9  19  10  20
    
>> reshape(c,5,4)
ans =
    1    7   13   19
    6   12   18    5
   11   17    4   10
   16    3    9   15
    2    8   14   20

※ Error : product of two values number of elements of matrix

 

 

3) Dot operators

>> a=[4 -2 -4 7; 1 5 -3 2;6 -8 -5 -6; -7 3 0 1]
>> b=[2 4 -7 -4; 5 6 3 -2;1 -8 -5 -3; 0 -6 7 -1]

a =
     4    -2    -4     7
     1     5    -3     2
     6    -8    -5    -6
    -7     3     0     1


b =
     2     4    -7    -4
     5     6     3    -2
     1    -8    -5    -3
     0    -6     7    -1

 

(0) Usual matrix multiplication

>> a*b
ans =
    -6    -6    35    -7
    24    46    37    -7
   -33    52   -83    13
     1   -16    65    21

 

(1) Dot multiplication : Element-wise operation

>> a.*b
ans =
     8    -8    28   -28
     5    30    -9    -4
     6    64    25    18
     0   -18     0    -1

 

(2) Dot power : Element-wise operation 

>> a.^2
ans =
    16     4    16    49
     1    25     9     4
    36    64    25    36
    49     9     0     1

 

(3) Reciprocals (역수)

>> 1./a
ans =
    0.2500   -0.5000   -0.2500    0.1429
    1.0000    0.2000   -0.3333    0.5000
    0.1667   -0.1250   -0.2000   -0.1667
   -0.1429    0.3333       Inf    1.0000

 

4) Operators on matrices

- Matlab Functions work by applying the function to each element in turn

  Ex) Trigonometric and Exponential functions, and Logarithms

- Many iterations and repetitions can be done with vectorization rather than by using loops

 

 

2.4.3 Constructing matrices

1) zeros / ones

zeros(n) : a zeros matrix of size n x n

zeros(m,n) : a zeros matrix of size m x n

zeros(m,n,p,...) : an m x n x p x ... multidimensional array of zeros

zeros(a) : a zeros matrix of the same size as matrix a

m, n, p : number / a : matrix

 

2) rand / randn 

a random numbers matrix (Size 옵션은 zeros 위 3개 형태와 같음)

rand : numbers are taken from a uniform distribution on interval [0,1]

randn : numbers are taken from a normal distribution N~(0,1)

>> floor(10*rand(3))
ans =
     0     9     9
     2     9     9
     5     1     4

>> floor(100*randn(3,5))
ans =
    71   148    67   163    72
   -21   140  -121    48   -31
   -13   141    71   103    29

 

3) A matrix every element of which is a function of one of its indices - using dot operators

>> rows = (1:10)'*ones(1,10)

rows =

     1     1     1     1     1     1     1     1     1     1
     2     2     2     2     2     2     2     2     2     2
     3     3     3     3     3     3     3     3     3     3
     4     4     4     4     4     4     4     4     4     4
     5     5     5     5     5     5     5     5     5     5
     6     6     6     6     6     6     6     6     6     6
     7     7     7     7     7     7     7     7     7     7
     8     8     8     8     8     8     8     8     8     8
     9     9     9     9     9     9     9     9     9     9
    10    10    10    10    10    10    10    10    10    10
>> cols = ones(10,1)*(1:10)

cols =

     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
>> A = rows + cols -1

A =

     1     2     3     4     5     6     7     8     9    10
     2     3     4     5     6     7     8     9    10    11
     3     4     5     6     7     8     9    10    11    12
     4     5     6     7     8     9    10    11    12    13
     5     6     7     8     9    10    11    12    13    14
     6     7     8     9    10    11    12    13    14    15
     7     8     9    10    11    12    13    14    15    16
     8     9    10    11    12    13    14    15    16    17
     9    10    11    12    13    14    15    16    17    18
    10    11    12    13    14    15    16    17    18    19
>> [cols,rows] = meshgrid(1:10,1:10)

cols =

     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10
     1     2     3     4     5     6     7     8     9    10


rows =

     1     1     1     1     1     1     1     1     1     1
     2     2     2     2     2     2     2     2     2     2
     3     3     3     3     3     3     3     3     3     3
     4     4     4     4     4     4     4     4     4     4
     5     5     5     5     5     5     5     5     5     5
     6     6     6     6     6     6     6     6     6     6
     7     7     7     7     7     7     7     7     7     7
     8     8     8     8     8     8     8     8     8     8
     9     9     9     9     9     9     9     9     9     9
    10    10    10    10    10    10    10    10    10    10

 

2.4.4 Vectorizations

Vectorization : an operation carried out over an entire matrix or vector

Matlab is designed to perform vectorized commands very quickly ! 

 

(1) Very efficient replacement for Loops

>> tic, for i=1:10^6,sin(i); end,toc
경과 시간은 0.119952초입니다.

>> tic, i=1:10^6;sin(i); toc
경과 시간은 0.045064초입니다.

(2) To generate the first 10 Square numbers

>> [1:10].^2
ans =
    1 4 9 16 25 36 49 64 81 100

(3) Logical Operators : To obtain all positive elements of the matrix

>> a>0
ans =
    1 0 0 1
    1 1 0 1
    1 0 0 0
    0 1 0 1

 

 

2.5 Plots

(1) plot(x, y)

>> x=[0:0.1:2*pi];
>> plot(x,sin(x))

(2) plot(x, y1, 'symbol1', x, y2, 'symbol2')

>> plot(x,sin(x),'.', x,cos(x),'o')

 

2.6 Help in Matlab

(1) help / doc + <function>

>> help for

for - 지정된 횟수를 반복하는 for 루프
    지정된 횟수만큼 루프에서 명령문 그룹을 실행합니다.
    for index = values, statements, end
    참고 항목 break, colon, continue, end, if, parfor, return, switch
    for에 대한 문서
>> doc for

 

(2) lookfor + <topic>

Use 'lookfor' when you want to find help on a particular topic, but don’t know the function to use

>> doc help
>> doc for
>> lookfor exponential
exp                            - Exponential.
expm                           - Matrix exponential.
expint                         - Exponential integral function.
expmdemo1                      - Matrix exponential via Pade approximation.
expmdemo2                      - Matrix exponential via Taylor series.
expmdemo3                      - Matrix exponential via eigenvalues and eigenvectors.
quatexp                        - Calculate the exponential of a quaternion.
vivaldi                        - Creates a vivaldi notch antenna on a ground plane with exponential
sweeptone                      - Exponential swept sine signal
exp                            - Overload exponential for DataMatrix object.
ewcov                          - Asset covariance from return series with exponential weighting.
simByQuadExp                   - Simulate Bates sample paths by Quadratic-Exponential
simByQuadExp                   - SIMBYTRANSITION Simulate CIR sample paths with Quadratic-Exponential
simByQuadExp                   - Simulate Heston sample paths by Quadratic-Exponential
exp                            - @FINTS/EXP exponential of the values in a FINTS object.
cordiccexp                     - CORDIC-based approximation of complex exponential e^(j*THETA).
....

'DIP > Matlab' 카테고리의 다른 글

[Ch6] Spatial Filtering  (0) 2022.02.08
[Ch5] Pointing Processing  (0) 2022.02.07
[Ch4] Image Display  (0) 2022.01.29
[Ch3] Images and MATLAB  (0) 2022.01.28
[Ch1] Introduction  (0) 2022.01.25
댓글
공지사항