Difference between revisions of "MatlabIntro"

From CCRMA Wiki
Jump to: navigation, search
(Created page with 'Category:MATLAB == General tips == 1.Try to vectorize every operation if you can 1.')
 
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
 
[[Category:MATLAB]]
 
[[Category:MATLAB]]
 +
 +
== Basics ==
 +
 +
* Use semi-colon at the end of the line to prevent the output of the operation to be printed to the screen
 +
 +
* Comments are specified using % at the beginning of the comment. Only line comments are allowed (sorry, but no block-comments, although there's a shortcut in the editor to comment out all the selected lines)
 +
 +
<pre>
 +
% This is a comment
 +
x = 0; % this is another comment
 +
</pre>
 +
 +
* MATLAB is not strongly typed. The following sequence of commands is valid:
 +
 +
<pre>
 +
x = 1;
 +
x = "this is a string"
 +
</pre>
 +
 +
* Functions may return more than one value, and they can be of any type. For example:
 +
 +
<pre>
 +
[r,c] = size([ones(2)])
 +
</pre>
 +
 +
 +
== Vector and Matrices ==
 +
 +
* Use square brackets to define a vector/matrix
 +
 +
<pre>
 +
x = [1 2 3 4 5]
 +
</pre>
 +
 +
 +
* Inside a vector/matrix definition, commas or spaces separate elements row-wise. For example:
 +
 +
<pre>
 +
x = [1 2 3 4 5]
 +
</pre>
 +
 +
is the same as:
 +
 +
<pre>
 +
x = [1,2,3,4,5]
 +
</pre>
 +
 +
and they both generate a row vector. Semi-colons, instead, indicate the end of a row. For example, the following line will create a column vector:
 +
 +
<pre>
 +
x = [1;2;3;4;5]
 +
</pre>
 +
 +
 +
* You can combine both to create a matrix:
 +
 +
<pre>
 +
x = [1 2 3 4;2 3 4 5;3 4 5 6]
 +
</pre>
 +
 +
* Some useful vector/matrix operators are:
 +
 +
The following code snippets assume that x was defined as:
 +
<pre>
 +
x = [1 2;3 4]
 +
</pre>
 +
 +
** Use ' as a suffix to transpose the vector/matrix:
 +
<pre>
 +
>> x'
 +
 +
ans =
 +
 +
    1    3
 +
    2    4
 +
>>
 +
</pre>
 +
** The function size(x) will return the size of x: first number is the number of row and the second is the number of columns:
 +
<pre>
 +
>> size(x)
 +
 +
ans =
 +
 +
    2    2
 +
</pre>
 +
 +
** You can access individual elements of a matrix using parenthesis (first argument specifies the row and the second specifies the column):
 +
<pre>
 +
>> x(2,1)
 +
 +
ans =
 +
 +
    3
 +
</pre>
 +
 +
== Plotting ==
 +
 +
The basic command is plot(x), to plot the contents of x (if x is a matrix, then each column is plotted as a different line):
 +
 +
<pre>
 +
fs = 441000;
 +
t = 0:1/fs:0.01;
 +
plot(sin(2*pi*220*t));
 +
</pre>
 +
 +
You can also use plot(x,y):
 +
 +
<pre>
 +
plot(t, sin(2*pi*220*t));
 +
</pre>
 +
 +
By default plot will erase the previous plot before drawing the new one. Use hold to prevent this from happening. Also, you can use a third argument to specify the line color (and other plotting options):
 +
 +
<pre>
 +
hold on;
 +
plot(t, sin(2*pi*110*t), 'r');
 +
plot(t, sin(2*pi*330*t), 'g');
 +
plot(t, sin(2*pi*440*t), 'k');
 +
hold off
 +
</pre>
 +
 +
== Sound ==
 +
 +
  
 
== General tips ==
 
== General tips ==
  
1.Try to vectorize every operation if you can
+
# Try to vectorize every operation if you can
1.
+
#

Latest revision as of 16:09, 22 September 2010


Basics

  • Use semi-colon at the end of the line to prevent the output of the operation to be printed to the screen
  • Comments are specified using % at the beginning of the comment. Only line comments are allowed (sorry, but no block-comments, although there's a shortcut in the editor to comment out all the selected lines)
% This is a comment
x = 0; % this is another comment
  • MATLAB is not strongly typed. The following sequence of commands is valid:
x = 1;
x = "this is a string"
  • Functions may return more than one value, and they can be of any type. For example:
[r,c] = size([ones(2)])


Vector and Matrices

  • Use square brackets to define a vector/matrix
x = [1 2 3 4 5]


  • Inside a vector/matrix definition, commas or spaces separate elements row-wise. For example:
x = [1 2 3 4 5]

is the same as:

x = [1,2,3,4,5]

and they both generate a row vector. Semi-colons, instead, indicate the end of a row. For example, the following line will create a column vector:

x = [1;2;3;4;5]


  • You can combine both to create a matrix:
x = [1 2 3 4;2 3 4 5;3 4 5 6]
  • Some useful vector/matrix operators are:

The following code snippets assume that x was defined as:

x = [1 2;3 4]
    • Use ' as a suffix to transpose the vector/matrix:
>> x'

ans =

     1     3
     2     4
>> 
    • The function size(x) will return the size of x: first number is the number of row and the second is the number of columns:
>> size(x)

ans =

     2     2
    • You can access individual elements of a matrix using parenthesis (first argument specifies the row and the second specifies the column):
>> x(2,1)

ans =

     3

Plotting

The basic command is plot(x), to plot the contents of x (if x is a matrix, then each column is plotted as a different line):

fs = 441000;
t = 0:1/fs:0.01;
plot(sin(2*pi*220*t));

You can also use plot(x,y):

plot(t, sin(2*pi*220*t));

By default plot will erase the previous plot before drawing the new one. Use hold to prevent this from happening. Also, you can use a third argument to specify the line color (and other plotting options):

hold on;
plot(t, sin(2*pi*110*t), 'r');
plot(t, sin(2*pi*330*t), 'g');
plot(t, sin(2*pi*440*t), 'k');
hold off

Sound

General tips

  1. Try to vectorize every operation if you can