OtherPapers.com - Other Term Papers and Free Essays
Search

Matlab Cheatsheet

Essay by   •  March 15, 2016  •  Study Guide  •  910 Words (4 Pages)  •  1,601 Views

Essay Preview: Matlab Cheatsheet

Report this essay
Page 1 of 4
  1. Basics
  • Types: int(8), int(16), int(32)
  1. Ex. range int8: intmin(‘int8’) = -128, intmax(‘int8’) = 127 [i.e. tot = 2^8]
  2. Typecasting ex: var_new = int32(var)
  • Elementary functions
  1. fix(rounds to int. toward zero),floor(rounds to next lower int.),ceil(rounds to next higher int.),round(rounds to next int.)
  2. nthroot(x,n), log(x)=ln(x), log10(x)=log10(x), exp(n)
  • Random numbers
  1. rand [one random number 0:1] rand*(high-low)+low, randi([low,high], …)
  • Characters / encoding (ASCII 128 characters); double(‘c’)=99, char(99)=c
  • Logical operators and precedence: (),^, */, +-, <=>,&&,||,=

  1. Vectors and matrices
  • linspace(x,y,n), logspace(x,y,n) [from 10^x to 10^y, ex: 10 100 1000 …), default n: respect. 100, 50
  • indexing in matrices  linear (column-by-column)
  • adding elements to matrices; add colon: mat(:,3) = [1 2]’, add row: mat(4,:) = 1:3
  • dimesions: length(x), [r,c]=size(x), numel(x), end(x)[last element in a vector, colon, row]
  • transpose (’) for matrices: first column  first row,…
  • change dimensions: fliplr(x),flipud(x),rot90(x,*n), repmat(mat,r,c)  rxc matrix of copies of mat,  reshape(x,r,c)  creates new matrix by iterating columnwise
  • remove entire r/c from matrices: mat(:,2) = [] / elim. elem. from vec: vec(1) = []
  • sum(x), prod(x) [=sum/product of all elements],
  1. for vec: all functions, incl. cumsum(x),cumprod(x) return same length as original, ex: vec=1 2 3 cumsum(vec)= 1 3 6
  2. for mat: all func operate on each column, return row vector with result for each c, cumsum&cumprod return matrix of same size , trick: to operate rowwise: fn(mat), for entire matrix: max(max(mat)),
  • scalar operations:
  1. numerical: mat*2, vec*2 (run action for each element)
  2. array (i.e. mult. two vecs/mets): have to have same size, use the dot .* for multi/div/exp
  3. matrix: [A]m x n * [B]n x p = [C]m x p  [pic 1](for vectors: row*colon=scalar, colon*row=matrix
  • logical operations:
  1. create logical vecs (but can idex back in the matrix)
  2. any(x), all(x) [if any/all elements are true], find(vec)[returns indices of numbers that are true, for mats linear index] ex: find(vec > 5) = 3 4 …, isequal(vec1,vec2) [true only if all elements are identical]
  • other: diff(x)[differences between consec. elements, ex: diff([1 5 7 12]) = 4 2 5, for mats operates columnwise, [x y] = meshgrid(vec1, vec2)

  1. Scripts
  • Input  remember to put ‘s’ if char (if type in variable name it’s ok even without ‘ ‘!)
  • fprintf(‘ %5.2f [OR f,s,c]\n’, vec), decimals have precedence over field width, %-5d (left justified), for mats: fprintf(‘%d\n’,mat) will print all in one column with linear ind. Solution: for a mat2x3: fprintf(‘%d %d %d\n’,mat)  disp(mat) easier
  • plots: plot(x,y, ‘r*’) [k black, o circle, s square..], bar chart: bar(x,y)
  1. ylabel(‘bla’), xlabel(‘bla’), title(‘bla’), axis([xmin  xmax  ymin  ymax])  ex: with variables axis([x-2  x+2  y-10  y+10])
  2. hold to superimpose two graphs, figure(1),figure(2) to open two graphs in two windows
  • save filename.dat variablename –ascii (save), save filename.dat variablename2 –ascii –append (append), load filename.dat (load)
  1. Strings
  • Treated as vectors of charaters // can form a matrix, but words of same length (!)
  • Horizontal conc: strcat(first,last) (gets rid of trailing blanks),  vertical: char(first,last) (creates matrix & adds trailing blanks for short words)
  • deblank (trailing), strtrim(trailing,leading)
  • compare: str(n)cmp(i) [i: ignores case, n: first n characters] ex: strncmpi(‘hey’,’HEYK’,3) = 1, using == is case sensible and returns logical vector
  • strfind(string, substring) returns index of beginning of substring, case sensitive (!)
  • strrep(string, oldsubstring, newsubstring) finds all occu. of a substring and replaces them
  • strtok(string, delimiter) cuts the string in two pieces, e.g. strtok(‘ciao’,a)  ci|ao
  • isletter, isspace: for a string they return a logical value for each character, ischar: only 1 or 0
  • str2num, e.g. str2num(‘1 2 3’) >> [1 2 3] (vector)
  1. Cell Arrays (matrix or vector)
  • Use cell(r,c) to preallocate
  • Indexing: mycell = {23, ‘a’}  mycell{1} = 23 (type: double), mycell(1) = [23] (type: cell)
  • Various: Delete element: mycell(2) = [] ; cellstr(from char array to cell, removes blanks), iscellstr(1 if only strings in cell)
  1. Structures
  • add field to ‘mystruct’: mysctruct.newfield = 111, remove a field: rmfield(mystruct, ‘fieldna’) but temporarily
  • can be passed to user-defined functions but not to built-in ones
  • isstruct, isfield(mystruct,’fieldname’), fieldnames(mystruct) returns a cell array with names of fields
  • create vectors of structures: mystruct(2) [ineff] // start from last element then define the previous // use repmat to create vector of a struct
  1. Subplot:

[pic 2] 

  1. Persistent variables:

[pic 3]

  1. Computational vs. vectorized code:

[pic 4] [pic 5][pic 6]

...

...

Download as:   txt (5.2 Kb)   pdf (753.5 Kb)   docx (661.7 Kb)  
Continue for 3 more pages »
Only available on OtherPapers.com