Matlab Basics: Yaara Erez MRC Cognition and Brain Sciences Unit November 2013
Matlab Basics: Yaara Erez MRC Cognition and Brain Sciences Unit November 2013
Yaara Erez
Easy graphics.
Why is programming important?
Matlab basics
- Variables
- Matlab programming environment
- Editor, scripts, functions
3 1 7 9 4
Matrices
Matrix 2D array (table).
Elements are ordered in 2 dimensions: rows and columns.
M x N matrix M rows, N columns.
Example:
myFirstMat = [1 2 3; 4 5 6];
N=4
1 2 3
4 5 6
M=5
Matrices - indexing
Indexing: the aij element is the element in the i-th row and
the j-th column.
a11 a1n
Example:
5 4 a amn
8 12
m1
7 1 9 3
11 5 2 13
a23
3 6 10 8
Arrays
Vector one-dimensional array.
Row: 1 x N array.
Column: N x 1 array
Matrix two-dimensional array
Table with rows and columns: M x N
Three-dimensional array/matrix a rectangular coboid of
elements.
Dimensions M x N x K.
Multi-dimensional arrays/matrices
All these arrays are simply the same data-type in Matlab, with
just different dimensions, or size.
A few more notes about arrays
help name_of_function
lookfor keyword
helpdesk
Internet
Examples
MatlabBasics.m
MatlabBasicsExtra.m
Practice 1
Open Matlab and change the current directory to a folder of your
choice.
In the command window, do the following:
Create a 1x5 array with numeric values as you like.
Find the variable in the workspace and double-click it to see its
content.
Change the value of the 3rd element in the array. Make sure you can
see this change in the workspace.
Delete the 4th element in the array.
Use size function to check for the size of the array.
Use length function to check for the length of the array.
Clear all the variables and command window using clear and clc.
Practice 2
Create a Matlab code file and save it in your current directory. In
this file, do the following:
Create a 3x4 matrix with values as you like.
Change the value of the element in the 2nd row and 3rd column.
Change all the values in the 2nd column at once by assigning a new
vector.
Swap columns 1 and 3.
Delete the 4th column.
Use size function to check for the size of the matrix.
Practice 3
In the command window, do the following:
Create a text variable that contains one word.
Create another text variable that contains one or more words.
Concatenate the two strings to create a third variable.
Display one of the strings in the command window using disp
function.