Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Overview

This is a simple tutorial to get you started with Matlab. Matlab makes it easy to perform scientific computations without having to learn a programming language such as Fortran, C, or C++. This tutorial is developed keeping assignment_0.pdf for AM205 in mind. It can be used for other classes or self-learning as well.

...

Using Matlab

Opening Matlab:

This depends on your operating system. On Mac, it is usually installed in /Applications and you should see a Matlab icon with a MATLAB_<version number>. Double clicking the icon should open it. On Windows, there should either be a shortcut on the desktop or it can be found under programs in the start-up.

Matlab Development Environment (Matlab Desktop):

This can change with new releases. The following video gives a quick introduction: 

http://www.mathworks.com/videos/working-in-the-development-environment-69021.html?s_cid=learn_vid

The key items are "Command Window" (where you type commands in this tutorial), "Current Folder", and the "Workspace".

Getting Help

Note: In Matlab, any line beginning with % is a comment (not executed by matlab).  Lines without % are commands that are executed by matlab. You can copy and paste the matlab statements in this tutorial into matlab

...

Code Block
function [m,val] = cheby(k,x)
% Chebyshev polynomial recurrent relation
% Relevant to question 2 in assignment_0
% Note: x and k are not vectors.
if k==0  % if ... elseif.. block controls the flow 
         % based on the value of k in this example.
 m=0;
 val=1.0;
elseif k==1
 m=1;
 val=x;
elseif k>=2
 tch(1)=1;
 tch(2)=x;
 for i=3:k+1
  tch(i)=2*x*tch(i-1)-tch(i-2);
 end
 m=i-1;
 val=tch(i);
end

...

Code Block
%%The primary functions for loading images is 'imread' and for displaying images is 'imshow'.
 
im=imread('baboon.png');
imshow(im);
 
%% Images are basically two dimensional array of pixels (picture elements) with a third dimension
%% that shows the intensity of pixel. If the image is a gray scale image,, the intensity is a single number
%% at each pixel. If it is a color image, it is stored as a "third dimension" with 3 value
%% each corresponding to the intensity of r (red), g (green) and b (blue) colors. Thus if we do:
 
size(im)
 
%% The result is 512 512 3, which indicates it is a two dimensional array of 512 x  512 pixels with three colors.
%% To get the 'red' intensity, you can do:
 
r=im(:, :, 1);
 
%% This is now a two dimensional array. The color intensity can vary anywhere from 8 bits (256 levels)
%% for grayscale to 24 bits (8 bits for each color) for color images. There are even 30-, 36-, or 48- bit
%% images. As far as Matlab is concerned,. the image once loaded is a three dimensional array with the last 
%% dimension taking on only 3 values. 


max(r(:)),min(r(:))
 
%% will show 255 and 0 (the red color is stored in 8 bits).


imshow(r)
 
%% will show the image in grayscale with intensity corresponding to "red" color intensity in the 
%% original image. You can convert the color image to a grayscale image:
 
img=rgb2gray(im);
imshow(img)
 
This is now a two dimensional image with a grayscale intensity of 8 bits. We have compressed the image
at the expense of color!

Grayscale Images:

Code Block
%% Many medical images (such as MRI, CT, ultrasound, etc.) are likely to be grayscale images. 
%% They can 10 or 12 bit images and it is convenient to represent them using 16 bits as computers can 16 bits efficiently.
A=imread('Brain-MRI.jpg');
size(A)
imshow(A)
%% We can convert this to an grayscale image with:
B=rgb2gray(A)
%% This will be a 2D array of pixels with gray shades represented by 8 bits (256 shades).
%% We can plot a histogram of color intensity (x-axis intensity, y axis -- number of pixels) with:
imhist(B)
%% We can reduce the variation in the intensity among the pixels (and thereby visually enhance the darker regions)
%% by using the matlab function histeq.
histeq(B)
%%will display the image with formerly darker regions more bright and brighter regions darker.


Additional tutorial material: fwdmatlabtutorialsetc.zip