octave cheatsheet.txt 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. BASICS:
  2. ";" after command supresses output of the result
  3. help cmd # writes help for cmd command
  4. lookfor str # search documentation for str
  5. pkg load package # load given package
  6. # comment
  7. v = [1, 3, 2] # creates vector
  8. m = [1, 2; 3, 4] # creates matrix
  9. v(1) # indexing (1-based), gets the first element
  10. v(2:5) # slice, get elements from 2 to 5
  11. v(end + 1) = 0 # append 0 to vector
  12. v(1:2:end) # select only odd elements of the vector
  13. m(1,1) # gets the matrix element at [1,1]
  14. m(1,:) # gets the first matrix row
  15. m(:,1) # gets the first matrix column
  16. 1:10 # creates a vector 1, 2, 3, ..., 10
  17. 1:0.5:10 # creates a vector 1, 1.5, 2, 2.5, ..., 10
  18. for a = v, disp(a); end # for loop, displays elements of vector "v"
  19. a = 0; while a < 10, a += 1, end # while loop
  20. if a == 10, disp(1); elseif a == 2, disp(2); else, disp(3); endif # if statement
  21. function r = f(x), if x > 0, r = 1; else r = 0; endif, endfunction # function definition
  22. f(3) # function call
  23. @f # function reference (can be given as an argument etc.)
  24. function [a,b] = f2(x), a = x; b = x; endfunction # function with multiple return values
  25. [X,Y] = meshgrid(1:10,1:10) # creates a 2D array as a function of x and y
  26. m2 = bsxfun(@f2,X,Y)
  27. OPERATORS:
  28. +
  29. -
  30. *
  31. /
  32. \ # reverse division (for matrices)
  33. ^
  34. ./ # element-wise divide
  35. .* # element-wise multiply
  36. .^ # element-wise power
  37. ' # transpose
  38. CONSTANT:
  39. pi
  40. e
  41. i
  42. inf
  43. nan
  44. realmax
  45. realmin
  46. BASIC FUNCTIONS:
  47. mod(x,y) Remainder, see also rem function.
  48. ceil(x)
  49. floor(x)
  50. round(x)
  51. sin(x)
  52. cos(x)
  53. tan(x)
  54. acos(x)
  55. asin(x)
  56. atan(x)
  57. sqrt(x)
  58. exp(x)
  59. abs(x)
  60. log(x) Natural logarithm (base = e).
  61. log10(x) Decadic logarithm (base = 10).
  62. sign(x)
  63. chdir(pathstr) Changes directory to "pathstr", e.g. "~/tmp".
  64. ADVANCED FUNCTIONS:
  65. arg(compl) Returns the argument of a complex number.
  66. conv(p,q) Multiplies two polynopmials represented by vectors of their coefficients.
  67. columns(mat) Returns the number of columns (width) of given matrix.
  68. dct(vec) Computes discrete cosing transform of given vector.
  69. dct2(mat) Computes 2D discrete cosing transform of given matrix.
  70. deconv(p,q) Divides two polynopmials represented by vectors of their coefficients.
  71. disp(what) Displays "what" according to currently set format (see format function).
  72. det(mat) Computes determinant of "mat" matrix.
  73. dot(vec) Computes a dot product.
  74. double(what) Converts given element to double (float) data type.
  75. eps(x) Machine-dependent, return floating point distance from x to the next number.
  76. eye(n) Creates n * n identity matrix.
  77. ezplot(f,dom,n) Plots a 2D curve defined by 2D function in given domain (vector [x_min,x_max,y_min,y_max]) with given number of points
  78. fclose(file) Closes file with given descriptor.
  79. fft(vec) Computes fast Fourier transform of given vector.
  80. fft2(mat) Computes 2D fast Fourier transform of given matrix.
  81. fliplr(mat) Flips matrix "mat" horizontally.
  82. flipud(mat) Flips matrix "mat" vertically.
  83. fopen(filename,mode) Opens file "filename" with given mode ('r','w' or 'a') and returns it descriptor.
  84. fplot(f,x1,x2,y1,y2,eps) Plots given function (use @ function reference) in given bounds and given precision
  85. idct(vec) Computer inverse cosine transform of given vector.
  86. idct2(mat) Computer inverse 2D cosine transform of given vector.
  87. ifft(vec) Computes inverse fast Fourier transform of given vector.
  88. ifft2(mat) Computes inverse 2D fast Fourier transform of given matrix.
  89. intersection(a,b) Creates an intersection of "a" and "b" sets.
  90. imread(path) Reads given image (can be filename or web url) into a matrix.
  91. imshow(img) Shows given image saved in matrix (datatype does matter for the display!).
  92. imwrite(img, path) Saves given image (which is stored as a matrix).
  93. int8(what) Converts given element to int8 data type.
  94. inv(mat) Computes matrix inverse.
  95. ismember(val,set) Checks if there is given value in given set (vector).
  96. linspace(from, to, num) Creates a vector of num values from "from" to "to".
  97. logspace(from, to, num) Creates a vector of num values from "from" to "to" in log space.
  98. mesh(z) Plots a 2D function (creates 3D plot) from a 2D matrix (array) of Z values
  99. numel(what) Returns the number of elements (length) of given object.
  100. ones(x,y) Creates x * y matrix filled with 1s.
  101. plot(vals) Plots "vals" vector as a function.
  102. plot(xvals, yvals) Plots points specified by "xvals" and "yvals" vectors.
  103. polyder(pol) Derivates polynomial represented by coefficients.
  104. polyfit(xvals,yvals,deg) Returns a polynomial of "deg" degree that best fits given data ("xvals", "yvals") - least square method.
  105. polyint(pol) Integrates polynomial represented by coefficients.
  106. polyout(vec,varstr) Displays a polynomial based on coefficients ("vec") and variable ("varstr", e.g. "x").
  107. polyval(vec,val) Evaluates a polynomial represented by coefficients ("vec") and variable value ("val").
  108. rand(x,y) Creates x * y matrix filled with random numbers from 0 to 1.
  109. rgb2gray(img) Converts given RGB image (saved in matrix) to grayscale.
  110. roots(pol) Finds roots of polynomial specified by its coefficients.
  111. rot90(mat) Rotates matrix "mat" by 90 degrees.
  112. rows(mat) Returns the number of rows (height) of given matrix.
  113. setdiff(a,b) Creates a difference of "a" and "b" sets.
  114. sort(vec) Sorts vector "vec".
  115. soundc(s,freq) Plays sound from vector "s" with "freq" sampling frequency.
  116. uint8(what) Converts given element to uint8 data type.
  117. union(a,b,) Creates an union of "a" and "b" sets.
  118. unique(vec) Removes duplicates from given vector.
  119. wavread(filename) Reads sound into vector from given file)
  120. wavwrite(s,freq,filename) Writes given audio vector to specified file.
  121. zeros(x,y) Creates x * y matrix filled with 0s.
  122. EXAMPLES:
  123. // apply low pass filter to an image (bigger than 20x20)
  124. img = rgb2gray(imread("test.png"))
  125. filter = resize(ones(20,20),rows(img),columns(img))
  126. imshow(uint8(idct2(dct2(img) .* filter)))