Benhur Llanza's Virtual Lecture Room
Home | ECE 512 (uP Sys) | ECE 514 (Comp. Apps) | Links | Visitor's Response Form
Some Useful Operations of Matlab

Some Operations on Matlab 

4    Some useful operations on complex numbers:

 

4    Complex scalar                         >> x = 3+4j

4    Real part of x                            >> real(x)         ->3

4    Imaginary part of x                    >> imag(x)       ->4

4    Magnitude of x                         >> abs(x)         ->5

4    Angle of x                                 >> angle(x)       ->0.9273

4    Complex conjugate of x            >> conj(x)        ->3 - 4i

 

>> x=[a:step:b]

4    Generate a vector that takes on the values a to b in increments of step

 

>> x=linspace(a,b,n)

4    generates a row vector x of n points linearly spaced between a and b

 

>> x=logspace(a,b,20)

4    generates  a logarithmically spaced vector x of n points between 10^a and 10^b.

 

 

4    Matrix building functions:

            >> A=zeros(m,n)

4    returns an m-by-n matrix of zeros

 

            >> A=ones(m,n)

4     returns an m-by-n matrix of 1s

 

            >> A=eye(m,n)

4     returns an m-by-n matrix with 1's on the diagonal and 0's elsewhere

 

 

>> A=rand(m,n)

4    returns an m-by-n matrix of random numbers whose elements are uniformly distributed in the interval (0,1)

>> A=randn(m,n)

4    returns an m-by-n matrix of random numbers  whose elements are normally distributed with mean 0 and variance 1

>> A=randint(m,n,range)

4    generates an m-by-n integer matrix. The entries are uniformly distributed and independently chosen from the range:

4    [0, range-1] if range is a positive integer

4    [range+1, 0] if range is a negative integer

 

 

4    Elements of a matrix are accessed by specifying the row and column

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

>> x=A(1,3)

4    Returns the element in the first row and third column

>> y=A(2,:)

4    Returns the entire second row [4 5 6]

4    “:” means “take all the entries in the column”

>> B=A(1:2,1:3)

4    Returns a submatrix of A consisting of rows 1 and 2 and all three columns [1 2 3; 4 5 6]

 

 

4    The basic arithmetic operations on matrices are:

 

4    + addition

4    - subtraction

4    * multiplication

4    / division

4    ^ power

4    ’ conjugate transpose

 

 

4    MATLAB provides element-by-element operations by prepending a ‘.’ before the operator

 

4    .* multiplication

4    ./ division

4    .^ power

4    .’ transpose (unconjugated)

 

 

4    MATLAB defines the following relational operations:

 

4    <          less than

4    <=        less than or equal to

4    >          greater than

4    >=        greater than or equal to

4    ==        equal to

4    ~=        not equal to

 

 

4    MATLAB defines the following logical operations:

 

4    & and

4    |  or

4    ~ not

 

 

4    The following functions operate element-wise when applied to a matrix:

 

                        sin                                cos                               tan

                        asin                              acos                             atan

                        sinh                              cosh                             tanh

                        exp                               log(natural log)  log10

                        abs                               sqrt                              sign

 

 

4    If statements

 

if expression

    statements

else

    statements

end

 

4    Example

 

If n<0

            a=a-1;

else

            a=a+1;

end

 

 

4    For

4    Repeats a group of statements a fixed, predetermined number of times.

 

                                    a=0;

                                    for n = 1:10

                                                a=a+1;

                                    end

 

 

To simplify your matlab file structure, you can use functions. An example of how to use matlab functions is the following:

Main Matlab Program

 

SegEqRx = SegEqNew + 1*SegRx(DimC*TTaps+1:LRx-DimC*TTaps);

clear SegEqNew; clear SegEqOld;

[SAMPLE_CENTRAL, BIT_DETECTED] = =syncronizer(SegEqRx,PNSEQ_VECTOR,NS_BIT,NBIT_PNSEQ);

 

Function declaration:

function [ SAMPLE_CENTRAL, BIT_DETECTED]

=syncro(SIGNAL,PNSEQ_VECTOR,NS_BIT,NBIT_PNSEQ);

Main of the function

 

 

>> plot(x,y)

4    produces a graph of y versus x, where x and y are two vectors

 

x=linspace(0,2*pi,100);

plot(x,sin(x));

 

 

 

4    It is possible to specify color, line styles, and markers when you plot your data using the plot command

plot(x,y,'color_style_marker')

4    color_style_marker is a string containing from one to four characters constructed from a color, a line style, and a marker type:

4    Color strings: 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'. These correspond to cyan, magenta, yellow, red, green, blue, white, and black

4    Linestyle strings are '-' for solid, '--' for dashed, ':' for dotted, '-.' for dash-dot, and 'none' for no line

4    The marker types are '+', 'o', '*' and 'x'

 

4    xlabel('string')

4    labels the x-axis of the current axes

 

4    ylabel('string')

4    labels the y-axis of the current axes

 

4    Title(‘string’)

4    add a title to a graph at the MATLAB command prompt or from an M-file

 

 

4    MATLAB directs graphics output to a figure window

4    Graphics functions automatically create new figure windows if none currently exist

            >>figure

4    creates a new window and makes it the current figure

            >>figure(h)

4    make an existing figure current by passing its handle (the number indicated in the window title bar), as an argument to figure

            >>clf

4    Clear current figure window

 

 

4    Setting hold to on, MATLAB doesn’t remove the existing graph and adds the new data to the current graph

 

x=linspace(0,2*pi,100);

plot(x,sin(x));

hold on;

plot(x,cos(x));

xlabel('x');

ylabel('Sine of x');

 

 

With more graphics functionalities:

 

x=linspace(0,2*pi,100);

plot(x,sin(x),’-ob’);

hold on; grid on;

plot(x,cos(x),’->r’);

xlabel('x');

ylabel('Sine of x');

legend('sin(x)','cos(x)',3)

 

 

 

4    Plot     

4    Graph 2-D data with linear scales for both axes

4    Loglog

4    Graph with logarithmic scales for both axes

4    Semilogx

4    Graph with a logarithmic scale for the x-axis and a linear scale for the y-axis

4    Semilogy

4    Graph with a logarithmic scale for the y-axis and a linear scale for the x-axis

 

4    bar(x,Y)

4    draws a bar for each element in Y at locations specified in x, where x is a monotonically increasing vector defining the x-axis intervals for the vertical bars

 

 

            >> bar((1:1:10),(1:1:10))

 

 

 

4    Stem

4    displays data as lines (stems) terminated with a marker symbol at each data value

 

 

                        x=linspace(0,2*pi,10);

                        stem(x,sin(x));

 

 

4    Stairs

4    Stairstep plots are useful for drawing time-history plots of digitally sampled data systems

 

 

                        x=linspace(0,2*pi,20);

                        stairs(x,sin(x));

 

 

Enter commands you see here in Matlab for a better feel on using it.

2005