BogoToBogo
  • Home
  • About
  • Big Data
  • Machine Learning
  • AngularJS
  • Python
  • C++
  • go
  • DevOps
  • Kubernetes
  • Algorithms
  • More...
    • Qt 5
    • Linux
    • FFmpeg
    • Matlab
    • Django 1.8
    • Ruby On Rails
    • HTML5 & CSS

Matlab Tutorial : Vectors and Matrices

Matlab_logo.png




Bookmark and Share





bogotobogo.com site search:




Vectors

MATLAB is based on matrix and vector algebra. So, even scalars are treated as $1 \times 1$ matrices.

We have two ways to define vectors:

  1. Arbitrary element (not equally spaced elements):
    >> v = [1 2 5];
    
    This creates a $1 \times 3$ vector with elements 1, 2 and 5. Note that commas could have been used in place of spaces to separate the elements, like this:
     
    >> v = [1, 2, 5];
    

    Keep in mind that we the index of the first element is 1 not 0:

    >> v(0)
    Subscript indices must either be real positive integers or logicals.
    >> v(1)
    ans =
         1
    

    If we want to add more elements:

    >> v(4) = 10;
    

    The yields the vector $v = [1,2,5,10]$.

    We can also use previously defined vector:

    >> >> w = [11 12];
    >> x = [v,w]
    

    Now, $x = [1, 2, 5, 10, 11, 12,]$

  2. Equally spaced elements :
    >> t = 0 : .5 : 3
    

    This yields $t=[0 ~ 0.5000 ~ 1.0000 ~ 1.5000 ~ 2.0000 ~ 2.5000 ~ 3.0000]$ which is a $1 \times 7$ vector. If we give only two numbers, then the increment is set to a default of 1:

    >> t = 0:3;
    

    This will creates a $1 \times 4$ vector with the elements 0, 1, 2, 3.

One more, adding vectors:

>> u = [1 2 3];
>> v = 4 5 6];
>> w = u + v
w =
     5     7     9


bogotobogo.com site search:


Column Vectors

We can create column vectors. In Matlab, we use semicolon(';') to separate columns:

>> RowVector = [1 2 3]   % 1 x 3 matrix
RowVector =
     1     2     3

>> ColVector = [1;2;3]   % 3 x 1 matrix
ColVector =
     1
     2
     3

>> M = [1 2 3; 4 5 6]    % 2 x 3 matrix

M =

     1     2     3
     4     5     6




Matrices

Matrices are defined by entering the elements row by row:

>> M = [1 2 3; 4 5 6]
M =
     1     2     3
     4     5     6

There are a number of special matrices:


null matrix M = [ ];
mxn matrix of zeros M = zeros(m,n);
mxn matrix of ones M = ones(m,n);
nxn identity matrix M = eyes(n);

If we want to assign a new value for a particular element of a matrix (2nd row, 3rd column):

M(2,3) = 99;



Accessing Matrix Element

We can access an element of a matrix in two ways:

  1. M(row, column)
  2. M(n-th_element)
>> M = [10 20 30 40 50; 60 70 80 90 100];
>> M
M =
    10    20    30    40    50
    60    70    80    90   100
 
>> M(2,4) % 2nd row 4th column
ans =
    90

>> M(8)  % 8th element 10, 60, 20, 70, 30, 80, 40, 90...
ans =
    90

Also, we can access several elements at once:

>> M(1:2, 3:4)  % row 1 2, column 3 4
ans =
    30    40
    80    90
>> M(5:8)   % from 5th to 8th elements
ans =
    30    80    40    90




Element based operation using dot('.')

If we use dot('.') on an operation, it means do it for all elements. The example does ^3 for all the elements of a matrix:

>> M = [1 2 3; 4 5 6; 7 8 9];
>> M
M =
     1     2     3
     4     5     6
     7     8     9

>> M.^3
ans =
     1     8    27
    64   125   216
   343   512   729

We can get inverse of each element:

>> 1./M
ans =
    1.0000    0.5000    0.3333
    0.2500    0.2000    0.1667
    0.1429    0.1250    0.1111




Inverse Matrix

We get inverse of a matrix using inv():

>> M = [1 2; 3 4]
M =
     1     2
     3     4

>> inv(M)
ans =
   -2.0000    1.0000
    1.5000   -0.5000




Transpose Matrix using a single quote(')

We get transpose of a matrix using a single quote("'"):

>> M = [1 2 3; 4 5 6; 7 8 9]
M =
     1     2     3
     4     5     6
     7     8     9

>> M'
ans =
     1     4     7
     2     5     8
     3     6     9




flipud() and fliplr()

flipud(): flip up/down:

>> M = [1 2 3; 4 5 6; 7 8 9]
M =
     1     2     3
     4     5     6
     7     8     9

>> flipud(M)
ans =
     7     8     9
     4     5     6
     1     2     3

fliplr(): flip left/right:

>> M
M =
     1     2     3
     4     5     6
     7     8     9

>> fliplr(M)
ans =

     3     2     1
     6     5     4
     9     8     7




Element rotation

We can do rotate elements, for instance, 90 degree counter-clock wise:

>> M

M =

     1     2     3
     4     5     6
     7     8     9

>> rot90(M)

ans =

     3     6     9
     2     5     8
     1     4     7




reshape()

We can make a change to the number of rows and columns as far as we keep the total number of elements:

>> M = [1 2 3 4; 5 6 7 8; 9 10 11 12]
M =
     1     2     3     4
     5     6     7     8
     9    10    11    12

>> reshape(M, 2, 6)  % Convert M to 2x6 matrix
ans =
     1     9     6     3    11     8
     5     2    10     7     4    12




Vector & Function

Functions are applied element by element:

>> t = 0:100;
>> f = cos(2*pi*t/length(t))
>> plot(t,f)

$f = cos(2 \pi t/length(t))$ creates a vector $f$ with elements equal to $2 \pi t/length(t)$ for $t = 0, 1, 2, ..., 100$.


Cosine.png







Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization

YouTubeMy YouTube channel

Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







Matlab Image and Video Processing



Vectors and Matrices

m-Files (Scripts)

For loop

Indexing and masking

Vectors and arrays with audio files

Manipulating Audio I

Manipulating Audio II

Introduction to FFT & DFT

Discrete Fourier Transform (DFT)



Digital Image Processing 2 - RGB image & indexed image

Digital Image Processing 3 - Grayscale image I

Digital Image Processing 4 - Grayscale image II (image data type and bit-plane)

Digital Image Processing 5 - Histogram equalization

Digital Image Processing 6 - Image Filter (Low pass filters)

Video Processing 1 - Object detection (tagging cars) by thresholding color

Video Processing 2 - Face Detection and CAMShift Tracking




Sponsor Open Source development activities and free contents for everyone.

Thank you.

- K Hong







OpenCV 3 -

image & video processing



Installing on Ubuntu 13

Mat(rix) object (Image Container)

Creating Mat objects

The core : Image - load, convert, and save

Smoothing Filters A - Average, Gaussian

Smoothing Filters B - Median, Bilateral






OpenCV 3 image and video processing with Python



OpenCV 3 with Python

Image - OpenCV BGR : Matplotlib RGB

Basic image operations - pixel access

iPython - Signal Processing with NumPy

Signal Processing with NumPy I - FFT and DFT for sine, square waves, unitpulse, and random signal

Signal Processing with NumPy II - Image Fourier Transform : FFT & DFT

Inverse Fourier Transform of an Image with low pass filter: cv2.idft()

Image Histogram

Video Capture and Switching colorspaces - RGB / HSV

Adaptive Thresholding - Otsu's clustering-based image thresholding

Edge Detection - Sobel and Laplacian Kernels

Canny Edge Detection

Hough Transform - Circles

Watershed Algorithm : Marker-based Segmentation I

Watershed Algorithm : Marker-based Segmentation II

Image noise reduction : Non-local Means denoising algorithm

Image object detection : Face detection using Haar Cascade Classifiers

Image segmentation - Foreground extraction Grabcut algorithm based on graph cuts

Image Reconstruction - Inpainting (Interpolation) - Fast Marching Methods

Video : Mean shift object tracking

Machine Learning : Clustering - K-Means clustering I

Machine Learning : Clustering - K-Means clustering II

Machine Learning : Classification - k-nearest neighbors (k-NN) algorithm










Contact

BogoToBogo
contactus@bogotobogo.com

Follow Bogotobogo

About Us

contactus@bogotobogo.com

YouTubeMy YouTube channel
Pacific Ave, San Francisco, CA 94115

Pacific Ave, San Francisco, CA 94115

Copyright © 2024, bogotobogo
Design: Web Master