In this article, I will provide a simple reference for Matlab and Octave. Octave is a GNU program designed to provide free tools similar to Matlab. I think there may not be 100% compatibility between Octave and Matlab, but I noticed that most basic commands are compatible. I will try to list those commands that can be used universally in Matlab and Octave. All the example code listed here is what I tried using Octave, not Matlab.
Although there are many functions that are not explained here, I will try to list those that are most commonly used in most Matlab sample scripts you can find. My intention is to provide you with a basic set of commands and examples so that you can at least read most of the sample scripts you can get from here and on the internet (eg, the internet) without it feeling overwhelming. If you are familiar with these minimal set of functions, you will have some understanding of the tool, and then you will understand the official documentation of Mathworks or GNU Octave, which explains all the functions but does not have many examples.
I haven’t finished what I consider to be a minimal collection yet, I hope I will in a few weeks. Please stay tuned!
Matrix (two-dimensional or higher-dimensional array)
Create matrix
common form
Identity matrix – eye()
M x N zero matrix – zeros()
M x N all-one matrix – ones()
computation
addition
Subtraction
Element-by-element multiplication – element by element
Inner Product Multiplication – inner Product
Power – element by element
Power – matrix
division
Transpose
Determinant
inverse matrix
Index element (reference element)
matrixname(rowindex,colindex)
matrixname(:,colindex)
matrixname(rowindex,:)
Matrix specific operations
Eigenvalues and eigenvectors
break down
LU decomposition
rearrange elements
circshift()
resize()
Method 1 : m = [ value value value ; value value value ; ...] Ex) Input m = [1 2 3; 4 5 6; 7 8 9] Output m = 1 2 3 4 5 6 7 8 9 Method 2: m = [vector; vector; ...] Ex) Input v1 = [1 2 3]; v2 = [4 5 6]; v3 = [7,8,9]; m = [v1; v2; v3] Output m = 1 2 3 4 5 6 7 8 9 < Creating an Identity Matrix > Method 1: m = eye(M) // M x M identity matrix Ex) Input m = one(3) Output m = 1 0 0 0 1 0 0 0 1 < Creating M x N Zero Matrix > Method 1: m = zeros(M,N) Ex) Input m = zeros(4,2) Output m = 0 0 0 0 0 0 0 0 Method 2: m = zeros(M) // M x M square matrix Ex) Input m = zeros(3) Output m = 0 0 0 0 0 0 0 0 0 < Creating a M x N one matrix > Method 1: m = ones(M,N) Ex) Input m = ones(4,2) Output m = 1 1 1 1 1 1 1 1 Method 2: m = ones(M) // M x M square matrix Ex) Input m = one(3) Output m = 1 1 1 1 1 1 1 1 1 <Mathematical Operation> Case 1: m = matrix1 + matrix 2; Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m2 = [9 8 7; 6 5 4; 3 2 1]; m = m1 + m2 Output m = 10 10 10 10 10 10 10 10 10 Case 2: m = matrix1 .* matrix 2; // element by element multiplication Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m2 = [9 8 7; 6 5 4; 3 2 1]; m = m1 .* m2 Output m = 9 16 21 24 25 24 21 16 9 Case 3: m = matrix1 * matrix 2; // Inner Product Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m2 = [9 8 7; 6 5 4; 3 2 1]; m = m1 * m2 Output m = 30 24 18 84 69 54 138 114 90 Case 4: m = matrix1 .^ n; // power of n for each element Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m2 = [9 8 7; 6 5 4; 3 2 1]; m = m1 .^ 2; Output m = 1 4 9 16 25 36 49 64 81 Case 5: m = matrix1 ^ n; // matrix power Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m2 = [9 8 7; 6 5 4; 3 2 1]; m = m1^2; Output m = 30 36 42 66 81 96 102 126 150 Case 6: tm = m'; // transpose matrix Ex) Input m = [1 2 3; 6 5 4; 7 3 9]; tm=m' Output tm = 1 6 7 2 5 3 3 4 9 Case 7: im = inv(m); // take the inverse matrix Ex) Input m = [1 2 3; 6 5 4; 7 3 9]; im=inv(m) Output im = -0.47143 0.12857 0.10000 0.37143 0.17143 -0.20000 0.24286 -0.15714 0.10000 Case 8: im = det(m); // take the determinant of the matrix Ex) Input m = [1 2 3; 6 5 4; 7 3 9]; dm=det(m) Output dm = -70 < Indexing the elements - matrixname(rowindex,colindex) > Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(1,2) Output ans = 2 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(1,2) = 0 Output m = 1 0 3 4 5 6 7 8 9 10 11 12 < Indexing the elements - matrixname(:,colindex) > Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(:,2) Output ans = 2 6 10 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(:,2) = 0 Output m = 1 0 3 4 5 0 7 8 9 0 11 12 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(:,2) = [15;16;17] Output m = 1 15 3 4 5 16 7 8 9 17 11 12 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(:,2) = [15 16 17]' Output m = 1 15 3 4 5 16 7 8 9 17 11 12 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(:,2) = [15 16 17].' Output m = 1 15 3 4 5 16 7 8 9 17 11 12 < Indexing the elements - matrixname(rowindex,:) > Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(2,:) Output ans = 5 6 7 8 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(2,:) = 0 Output m = 1 2 3 4 0 0 0 0 9 10 11 12 Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12]; m(2,:) = [15 16 17 18] Output m = 1 2 3 4 15 16 17 18 9 10 11 12 <Matrix Operators> Case 1: Eigenvalue and Eigenvector Ex) Input A = [1.01 0;0 0.9]; [v,d]=eig(A) Output v = 0 1 1 0 d = Diagonal Matrix 0.90000 0 0 1.01000 Input A*v Output ans = 0.00000 1.01000 0.90000 0.00000 Input v*d Output ans = 0.00000 1.01000 0.90000 0.00000 < Decomposition - LU Decomposition > Ex) Input m = [1 2 3 4;5 6 7 8;9 10 11 12;13 14 15 16] Output m = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 Input [L,U] = lu(m) Output L = 0.07692 1.00000 0.00000 0.00000 0.38462 0.66667 -0.63988 1.00000 0.69231 0.33333 1.00000 0.00000 1.00000 0.00000 0.00000 0.00000 U = 13.00000 14.00000 15.00000 16.00000 0.00000 0.92308 1.84615 2.76923 0.00000 0.00000 -0.00000 -0.00000 0.00000 0.00000 0.00000 0.00000 Input L*U Output ans = 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 < Rearranging Elements - circshift() > Case 1: m = circshift(m1,N) // Where N is a positive Number. This shift rows Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = circshift(m1,1) Output m = 7 8 9 1 2 3 4 5 6 Case 2: m = circshift(m1,N) // Where N is a Negative Number. This shift rows Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = circshift(m1,-1) Output m = 4 5 6 7 8 9 1 2 3 Case 3: m = circshift(m1,[0 N]) // Where N is a Positive Number. This shift cols Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = circshift(m1,[0 1]) Output m = 3 1 2 6 4 5 9 7 8 Case 3: m = circshift(m1,[0 N]) // Where N is a Negative Number. This shift cols Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = circshift(m1,[0 1]) Output m = 2 3 1 5 6 4 8 9 7 < Rearranging Eelemetns - resize() > Case 1: m = resize(m1,M,N,..) // Resize Matrix m1 to be an M x N x .. matrix, where M, N is smaller than the original matrix Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = resize(m1,3,2) Output m = 1 2 4 5 7 8 Case 2: m = resize(m1,M,N,..) // Resize Matrix m1 to be an M x N x .. matrix, where M or N is smaller than the original matrix Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = resize(m1,4,5) Output m = 1 2 3 0 0 4 5 6 0 0 7 8 9 0 0 0 0 0 0 0 Case 3: m = resize(m1,M) // Resize Matrix m1 to be an M x M matrix Ex) Input m1 = [1 2 3; 4 5 6; 7 8 9]; m = resize(m1,2) Output m = 1 2 4 5