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 nter/tutorials/launchpad.html

3 Naming variables >> x = 5 x = 5 >> xsq = x^2, xdiv = x/20 xsq = 25 xdiv = 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 = >> 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 = [ ] v = >> v(3) ans = 6 >> v2 = [2; 4; 6; 8] v2 = >> v2' ans =

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

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 = >> z2 = [3:8] z2 = >> y = [1:2:10] y = >> y2 = [4:.5:6] y2 =

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

8 Arithmetic, relational, and logical operations on vectors >> x = [5:5:25] x = >> y = [1:20:100] y = >> add = x' + y' add = >> multiply = x'.* y' multiply = >> compare = x' < y' compare = 0 1 >> both = y' < 50 & y' == 41 both = 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 = >> y = [1:20:100] y = 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 = >> m = [z; y] m =

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 = >> M2 = [2:2:6; 1:2:5; 7:9] M2 = >> 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 = >> M(:,1) ans = >> M2 = floor(5.*randn(3)) M2 = >> M2(3,:) ans = >> M + M2 ans = >> M2 <= M ans =

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

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 =

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 = >> c{2,2}(3,2) ans = 7 >> c{2,2}(3,2) = 99; c{2,2} ans =

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