# Differences

This shows you the differences between two versions of the page.

— |
ee370matlab [2012/08/13 13:29] (current) beard created |
||
---|---|---|---|

Line 1: | Line 1: | ||

+ | ====== 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. |