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 : Indexing & Masking

Matlab_logo.png




Bookmark and Share





bogotobogo.com site search:




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

We can access individual element by specifying (row, column):

>> M(2,3)
     6

Also, note that Matlab is using 1-based index not 0-based index.





Slicing arrays
>> 1:4
ans =
     1     2     3     4

We can take a portion of matrix using slice.

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

We took all rows and 3rd and 4th columns.

When we takes all rows or columns, we don't have to use specific indices, and we can use this form:

>> M2(:,3:4)
     9    13
    10    14
    11    15
    12    16


bogotobogo.com site search:

Shifting data using slice

The following example assigns value of the last three element to the first three elements:

>> v = 10:10:100
    10    20    30    40    50    60    70    80    90   100

>> v(1:3) = v(8:10)
    80    90   100    40    50    60    70    80    90   100




Array as subscripts
>> a = 10:10:100
    10    20    30    40    50    60    70    80    90   100

>> index = [1 10 5 7 9]
     1    10     5     7     9

>> b = a(index)
    10   100    50    70    90

In the code, we made a new array by using array as subscripts to the source array.





Comparison

We can compare each element with a value, and the output is a type of boolean not double:

>> a = 1:10
    1     2     3     4     5     6     7     8     9    10

>> b = a < 7
     1     1     1     1     1     1     0     0     0     0

We can use this to make another array:

>> new_a = a(a<7)
     1     2     3     4     5     6

The operation only takes elements that a<7 is true. Since we have a new array b, we can get the same result by doing this:

>> new_a = a(b)
     1     2     3     4     5     6

However, we cannot do this:

>> b2 = [1 1 1 1 1 1 0 0 0 0]
     1     1     1     1     1     1     0     0     0     0

>> new_a2 = a(b2)
Subscript indices must either be real positive integers or logicals.

That's because the elements of b2 is not a boolean but a double type.



Assigning using a mask

We can assign a value to only an element that the mask has true value:

>> a = [0 1 2 3];
>> mask = [true false true false];
>> a(mask) = [100 102]
a =
   100     1   102     3




Using mask while keeping the array size

Let's see how the masking changes the size of an array:

>> a = 0:10:100
     0    10    20    30    40    50    60    70    80    90   100

>> mask = a < 80
     1     1     1     1     1     1     1     1     0     0     0

>> a(mask)
     0    10    20    30    40    50    60    70

But we want to keep the size of an array unchanged while we can still applying the mask. We can achieve this by using dot(.) meaning every element:

>> a.*mask
     0    10    20    30    40    50    60    70     0     0     0

What it is doing is a element-wise multiplication with the mask!









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