Download Matlab Class 8 - Rutgers University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Dual graph wikipedia , lookup

Matrix calculus wikipedia , lookup

Median graph wikipedia , lookup

Signal-flow graph wikipedia , lookup

Transcript
Matlab Class 8
Xiaotao Su, Ph.D.
Visual Psychophysicist & IT Group Leader
Rutgers Center for Cognitive Science (RuCCS)
and
Chris Kourtev
RuCCS
Small Pieces
• Starting with matlab version 7.0 you can execute small chunks of
code
• This is called cells (nothing to do with cell arrays)
• %% mark off the beginning and end of cell region
• Cell regions are seen as yellow
• Pressing ctrl/cmd + return causes the workspace to execute the
command in the active cell
If you do not see a yellow region, in the menu
bar select Cell->Enable Cell Mode
RuCCS
Creating a vector of numbers
• This is similar to the way you create an iteration for a “for”
loop
• x = (0:20) ----> x = [0, 1, … 20]
• x = (0.1:0.1:1) ---> x = [0.1, 0.2, … , 1.0]
• x = (0:2:20) ----> x = [0, 2, 4, … 20]
• x = (0:2:20)’ ---> vertical vector like the previous one
RuCCS
discrete distributions and selecting segments
• you can use something like x=(0:20) to specify a discrete distribution
of 21 different possibilities
• to select the first 6 elements you could say
y = x(1:6) ---> y would be [0, 1 … 5]
• Or to select the last 4 elements
• y = x(end-3:end) ---> y would be [17, 18, 19, 20]
RuCCS
y = binopdf(x(1:6), 5, .8)
• used to generate a binomial distribution function the case in which
one flips a weighted coin
• .8 probability of getting heads
• there are 6 possibilities (0 heads, 1 head… 5 heads
• y gives the probability of each of these outcomes
This function requires the stats toolbox
RuCCS
Plotting
• Subplot will create a figure window for plotting
• subplot (2, 1, 2) will say we are going to create a (2 row, 1
column, …) figure. The last number specifies that we are
currently going to write in the second element of this
figure.
• plot(y) will create a graph on currently chosen section of
the figure (specified by the third parameter in subplot)
RuCCS
setting up the plot
•
•
•
•
•
xlim([1 5]) %sets the limits of the x axis
bar(y) %will use a bar graph
ylabel(‘Probability of n’)
xlabel(‘N’)
title(‘Discrete distributions should be plotted as histograms’)
RuCCS
more then one figure?
•
•
•
•
•
figure %opens a new figure window
All new subplots will go to the new figure
subplot(2, 1, 1)
bar(y)
xlim([1 21]) %we want 21 possibilities
RuCCS
more then one graph on a plot
• hold on %tells matlab to keep the previous graph and draw a
new one on top
• stairs(cumsum(y), ‘r’)
– cumsum(y) <--- each value is the sum of previous values of y
– ‘r’ makes the graph line red
– stairs will plot it as a staircase
• hold off % now any future graph additions will clear previous
graphs on this plot
• legend(‘distribution’, ‘cumsum’)
RuCCS
Using images in experiments
• Images are stored in matlab as
Width x Height x 3 matrix
x
y
R
G
B
RuCCS
file -> matrix and drawing it on the screen
• img = imread(‘winter.jpg’, ‘jpg’);
• You draw an image just like you would a rectangle:
• Screen(window, ‘PutImage’, img, [0, 0, 200, 200]);
RuCCS
Image info and structures
• img_info = imfinfo(‘winter.jpg’)
• img_info.Width will be the width of the image
• screen(window, ‘PutImage’, img,
[0, 0, img_info.Width, img_info.Height]);
RuCCS
Making sounds
• A sound is a 2 x N matrix where N is the number of bits that
make up the sound file and the two channels are for left and
right
RuCCS
Making sounds
• [sound, samplerate, samplesize] =
wavread(‘feedback.wav’);
sound = sound’;
Snd(‘Play’, sound, samplerate, samplesize);
Tip:
To make your own wav files I recommend
using an application called audacity
http://audacity.sourceforge.net/