BYU Home page BRIGHAM YOUNG UNIVERSITY  
Search BYU 
Contact   |   Help
Navigation Menu

[expand all...]
[COLLAPSE ALL...]



MATLAB Help

Useful commands

Here is a selection of useful MATLAB commands that can help make your life easier. The help pages for MATLAB are excellent. Many of them have a lot more functionality but we are showing them here as they are commonly used in 370. Type "help *" at the command line to get common operators such as "+" and "-".

bar(x, y)

Plots a bar graph of x values with heights of y.

clf

Clears your currently active figure.

conv(x,y)

Convolution of x vector with the y vector (make sure that they are both row vectors).

if,else,end

This allows you to do conditional statements. For example:
if (point(1,i) < 0.5) && (point(2,i) < 0.5)
    plot(point(1,i), point(2,i), 'xr')
    count = count + 1;
else
    plot(point(1,i), point(2,i), 'xb')
end

This code evaluates the top statement, that is if my point i is found less than x < 0.5 and y < 0.5. If it is, then it plots that point as a red "x" and counts it. If it is not, then it plots the point as a blue "x". You can use an "if" statement without the else. Make sure you include the "end"! To evaluate conditional statements, you need "relational operators" as indicated below.

for

This allows you to have a for a loop with an increment. For example:
N = 10;
count = 0;
for i=1:N
    count = count + 1;
end

This allows you to count from 1 to N, which in this case is 10. Your count is the number of times you passed through the for loop.

hist(x)

Plots a histogram of the values of x. It automatically divides the values into bins and then counts how many occur.

Multiplication

We may want to do specific things.
"*" is regular matrix multiplication.
".*" is the element by element multiplication.

plot(x, y)

Plots a graph of x values on the x-axis and y values on the y-axis. The default is for a line to connect the points. If you want to plot differently, for example, x's, then you can type in plot(x, y, 'xr') and it will plot the values with "x's" and the color red.

random

This for example can be used like X_vector=random('Poisson', 3, 1, 10) where this produces a vector of outcomes of a Poisson random variable with parameter 3 and vector of 1 row with 10 columns. This also works for 'Bionomial', 'Geometric' and others.

Relational Operators

"&&" and
"||" or
"==" equals (make sure they are of the same type, i.e. double)
"<" less than
"<=" less than or equal
">" greater than
">=" greater than or equal

sum

This sums up everything in your vector.

xlim([x1 x2])

This limits the range on your x axis of your figure to the interval x1 to x2.

ylim([y1 y2])

This limits the range on your y axis of your figure to the interval y1 to y2.

MATLAB Example

Joint Continuous Distributions (lecture on 24 February 2010): joint_continuous.m

Other MATLAB Examples

Bivariate normal distribution: Normal.m
MAP-Rule Example: MAP_rule_plot.m
Binary Markov Process Example: binary_markov.m
Bionomial Counting Process Example: binomial_counting.m
Discrete Random Walk Process: discrete_random_walk.m
Discete Time Wiener Process: DT_Wiener_process.m

Maintained by ECEn IMMERSE Web Team.
Copyright © 1994-2010. Brigham Young University. All Rights Reserved.