# Matlab Hints and Commands

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.