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 : For Loop

Matlab_logo.png




Bookmark and Share





bogotobogo.com site search:




Syntax

For loop's syntax looks like this:

for loop_index = vector
    code;
end

This will iterate each element of the vector.

Let's write our first for loop:

for i = 1:3
    i
end

The code will print out 1, 2 and 3.

Next try another one:

v = -3:3;
for i = 1:length(v)
    v(i)
end

If we run the code, we get -3, -2, ..., 2, 3

The following code will add up each element of a vector, and display the result after ending the for loop:

v = 1:10;
sum = 0;
for i = 1:length(v)
    sum = sum + v(i);
end
disp(sum)


bogotobogo.com site search:

Step size

We can specify the step size of the loop:

for loop_index = start: step_size : end
    code;
end

Suppose We want to add only odd numbers from the numbers in 1:10

sum = 0;
for i = 1:2:10
    sum = sum + i;
end
disp(sum)    % 25



vector for loop index

The vector for the loop index does not have to be in order:

a = 1:10;
indx = [1 9 2 4];
sum_a = 0;
for i = indx
    sum_a = sum_a + a(i);
end
disp(sum_a)  % 16

The loop will access the elements of vector a starting from the 1st, 9th, 2nd and 4th.

a = 1:10;
v = zeros(1,length(a));
sum_a = 0;
for i = 1:length(a)
    sum_a = sum_a + i;
    v(i) = sum_a;
end
figure;
plot(v);
sum_1_to_10.png

We can also check the result from the Variable window by double clicking the v on Workspace pane:

Variable_Window.png



Comment on/off

Sometime we want to have several lines be commented out or uncomment those lines. We have short-keys:

  1. Select the text
  2. Use Ctrl-R to comment
  3. Use Ctrl-T to uncomment








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