Presentation is loading. Please wait.

Presentation is loading. Please wait.

MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.

Similar presentations


Presentation on theme: "MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in."— Presentation transcript:

1 MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in a matrix Linear algebra, signal processing, image processing, statistics, fMRI, EEG, modeling, neural nets... MATLAB is dumb, you are smart

2 Good info online MathWorks- videos, links to university run tutorials/resources, examples: – Union College and Southern Illinois tutorials are very good http://www.mathworks.com/academia/student_ce nter/tutorials/launchpad.html

3 Naming variables >> x = 5 x = 5 >> xsq = x^2, xdiv = x/20 xsq = 25 xdiv = 0.2500 Can assign variable names to scalars, strings, matrices, function outputs, etc... >> st = 'string' st = string >> stArray = {'steph' 'felix' 'don' 'mike'}' stArray = 'steph' 'felix' 'don' 'mike' >> M = floor(randn(3,3)) M = -1 -1 1 -2 0 0 -2 -1 -2 >> dimM = size(M) dimM = 3 3

4 Vectors – A special kind of matrix Use square brackets to define, parentheses to index (MATLAB indexes from 1, not 0) Put spaces between numbers to create row vector, semi-colons to create column vector Apostrophe for transpose >> v = [2 4 6 8] v = 2 4 6 8 >> v(3) ans = 6 >> v2 = [2; 4; 6; 8] v2 = 2 4 6 8 >> v2' ans = 2 4 6 8

5 Vectors Add, replace, delete elements using indexing >> v = [2; 4; 6; 8] v = 2 4 6 8 >> v(7) = 10 v = 2 4 6 8 0 10 >> v(2) = 15 v = 2 15 6 8 0 10 >> v(4) = [ ] v = 2 15 6 0 10

6 Vectors When defining, use colon to define set of numbers with common increment, default increment is 1 Can define increment >> z = [1:5] z = 1 2 3 4 5 >> z2 = [3:8] z2 = 3 4 5 6 7 8 >> y = [1:2:10] y = 1 3 5 7 9 >> y2 = [4:.5:6] y2 = 4.0000 4.5000 5.0000 5.5000 6.0000

7 Vectors Access subsets of elements using colon, or a vector of indices >> z2 = [3:8] z2 = 3 4 5 6 7 8 >> z2(1:3)' ans = 3 4 5 >> z2(1:2:6)' ans = 3 5 7 >> z2(3:-1:1)' ans = 5 4 3 >> z2([1 2 5 6])' ans = 3 4 7 8

8 Arithmetic, relational, and logical operations on vectors >> x = [5:5:25] x = 5 10 15 20 25 >> y = [1:20:100] y = 1 21 41 61 81 >> add = x' + y' add = 6 31 56 81 106 >> multiply = x'.* y' multiply = 5 210 615 1220 2025 >> compare = x' < y' compare = 0 1 >> both = y' < 50 & y' == 41 both = 0 1 0 if multiplying or dividing elements of one vector by those of another, be sure to use. before * or /

9 Last words on vectors >> z = [1:5] z = 1 2 3 4 5 >> y = [1:20:100] y = 1 21 41 61 81 Can perform any operations on vector subsets Vectors are really just special instances of matrices – look what happens if I try to define a new column vector m, using the row vectors y and z → I get a 2 x 5 matrix >> z(1:2) + y(4:5) ans = 62 83 >> m = [z; y] m = 1 2 3 4 5 1 21 41 61 81

10 Matrices matrix = m x n array of numbers, where m is rows and n is columns Defined and indexed (but with one more dimension) the same way as vectors >> M = [2 4 6; 1 3 5; 7 8 9] M = 2 4 6 1 3 5 7 8 9 >> M2 = [2:2:6; 1:2:5; 7:9] M2 = 2 4 6 1 3 5 7 8 9 >> M(3,2) ans = 8 >> M2(1,3) ans = 6

11 Matrices colon operator denotes all rows or all columns arithmetic, relational, and logical operations >> M M = 2 4 6 1 3 5 7 8 9 >> M(:,1) ans = 2 1 7 >> M2 = floor(5.*randn(3)) M2 = 1 -5 2 3 3 6 -7 -1 2 >> M2(3,:) ans = -7 -1 2 >> M + M2 ans = 3 -1 8 4 6 11 0 7 11 >> M2 <= M ans = 1 1 1 0 1 0 1 1 1

12 Matrices Can be thought of in terms of vectors >> x =[1:5] x = 1 2 3 4 5 >> y = floor(2.*randn(1,5)) y = -2 1 -1 -2 4 >> z = [100:-20:20] z = 100 80 60 40 20 >> M = [x; y; z] M = 1 2 3 4 5 -2 1 -1 -2 4 100 80 60 40 20 >> M = [x(1:2); y(4:5); z([2 5])] M = 1 2 -2 4 80 20

13 A matrix of matrices called cells in MATLAB, use curly brackets >> c = {M M2 M+M2; M(:,1) M2(3,:) M2> c{2,2} ans = -7 -1 2

14 More complex data structures cell – array of matrices(any size/type), numerically indexed using curly brackets and parentheses >> c = cell(size(M)) c = [ ] [ ] [ ] >> c{2,2} = floor(5*randn(3,3)) c = [ ] [ ] [ ] [ ] [3x3 double] [ ] [ ] [ ] [ ] >> c{2,2} ans = -6 -2 -6 2 4 -1 1 7 -6 >> c{2,2}(3,2) ans = 7 >> c{2,2}(3,2) = 99; c{2,2} ans = -6 -2 -6 2 4 -1 1 99 -6

15 More complex data structures cont struct – similar to cell but not indexed, access elements through field names using dot, 'value' arrays must all be same size >> s = struct('type',{'big','little'},'color','red','x',{3 4}) s = 1x2 struct array with fields: type color x >> s.type ans = big ans = little >> s.color ans = red ans = red >> s.x ans = 3 ans = 4

16 Graphics examples >> hist(x) >> hist(x,20), xlabel('random numbers'), ylabel('count') >> x = 10.*randn(1,1000); >> scatter(x, x.^2), xlabel('x'), ylabel('x squared'),... title('scatter plot')

17 Graphics examples >> M M = 1 2 3 4 5 -2 1 -1 -2 4 100 80 60 40 20 >> boxplot(M') >> x = [1:10]; >> y = [10:-1:1]; >> figure >> subplot(1,2,1), plot(x,y) >> subplot(1,2,2), plot(x,y.^2)

18 Useful commands... help - Display help text in command window >> help general whos - List current variables, long form lookfor - Search all M-files for keyword what – List MATLAB related files which - Locate functions and files clear - Clear variables and functions from memory (be careful)

19 … pwd – Print working directory cd – change working directory dir – List directory contents path - Get/set search path addpath - Add directory to search path pathtool - View, modify, and save the MATLAB search path

20 … save - Save workspace variables to disk load - Load workspace variables from disk diary - Save text of MATLAB session isa - True if object is a given class struct - Create or convert to structure array type – List contents of m-file


Download ppt "MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in."

Similar presentations


Ads by Google