# Matlab Cheatsheet

Published: 2021-06-29 06:44:38

Category: Science

Type of paper: Essay

This essay has been submitted by a student. This is not an example of the work written by our professional essay writers.

Hey! We can write a custom essay for you.

All possible types of assignments. Written by academics

GET MY ESSAY
BasicsTypes: int(8), int(16), int(32)Ex. range int8: intmin(‘int8’) = -128, intmax(‘int8’) = 127 [i.e. tot = 2^8]Typecasting ex: var_new = int32(var)Elementary functionsfix(rounds to int. toward zero),floor(rounds to next lower int.),ceil(rounds to next higher int.),round(rounds to next int.)nthroot(x,n), log(x)=ln(x), log10(x)=log10(x), exp(n)Random numbersrand [one random number 0:1] rand*(high-low)+low, randi([low,high], …)Characters / encoding (ASCII 128 characters); double(‘c’)=99, char(99)=cLogical operators and precedence: (),^, */, +-, ,&&,||,=Vectors and matriceslinspace(x,y,n), logspace(x,y,n) [from 10^x to 10^y, ex: 10 100 1000 …), default n: respect. 100, 50indexing in matrices  linear (column-by-column)adding elements to matrices; add colon: mat(:,3) = [1 2]’, add row: mat(4,:) = 1:3dimesions: 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 columnwiseremove entire r/c from matrices: mat(:,2) = [] / elim. elem. from vec: vec(1) = []sum(x), prod(x) [=sum/product of all elements], for vec: all functions, incl. cumsum(x),cumprod(x) return same length as original, ex: vec=1 2 3 cumsum(vec)= 1 3 6 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: numerical: mat*2, vec*2 (run action for each element)array (i.e. mult. two vecs/mets): have to have same size, use the dot .* for multi/div/expmatrix: [A]m x n * [B]n x p = [C]m x p  [pic 1](for vectors: row*colon=scalar, colon*row=matrixlogical operations:create logical vecs (but can idex back in the matrix)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) ScriptsInput  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) easierplots: plot(x,y, ‘r*’) [k black, o circle, s square..], bar chart: bar(x,y)ylabel(‘bla’), xlabel(‘bla’), title(‘bla’), axis([xmin  xmax  ymin  ymax])  ex: with variables axis([x-2  x+2  y-10  y+10])hold to superimpose two graphs, figure(1),figure(2) to open two graphs in two windowssave filename.dat variablename –ascii (save), save filename.dat variablename2 –ascii –append (append), load filename.dat (load)StringsTreated 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 vectorstrfind(string, substring) returns index of beginning of substring, case sensitive (!)strrep(string, oldsubstring, newsubstring) finds all occu. of a substring and replaces themstrtok(string, delimiter) cuts the string in two pieces, e.g. strtok(‘ciao’,a) → ci|aoisletter, isspace: for a string they return a logical value for each character, ischar: only 1 or 0str2num, e.g. str2num(‘1 2 3’) >> [1 2 3] (vector)Cell Arrays (matrix or vector)Use cell(r,c) to preallocateIndexing: 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)Structuresadd field to ‘mystruct’: mysctruct.newfield = 111, remove a field: rmfield(mystruct, ‘fieldna’) but temporarilycan be passed to user-defined functions but not to built-in onesisstruct, isfield(mystruct,’fieldname’), fieldnames(mystruct) returns a cell array with names of fieldscreate vectors of structures: mystruct(2) [ineff] // start from last element then define the previous // use repmat to create vector of a structSubplot:[pic 2] Persistent variables:[pic 3]Computational vs. vectorized code:[pic 4] [pic 5][pic 6]

#### Warning! This essay is not original. Get 100% unique essay within 45 seconds!

GET UNIQUE ESSAY

We can write your paper just for 11.99\$

i want to copy...

This essay has been submitted by a student and contain not unique content