En este artículo, veremos cómo realizar algunas operaciones computacionales básicas en Octave. A continuación se muestra la lista de varias operaciones computacionales que se pueden realizar en Octave para usarlas en varios algoritmos de aprendizaje automático:
1. Operaciones con arrays: las arrays son los componentes centrales de Octave. Veamos algunas operaciones matriciales en Octave:
% declaring 3x3 matrices M1 = [1 2 3; 4 5 6; 7 8 9]; M2 = [11 22 33; 44 55 66; 77 88 99]; % declaring a 2x2 matrix M3 = [1 2; 1 2]; % matrix multiplication mat_mul = M1 * M2 % element wise multiplication of matrices ele_mul = M1 .* M2 % element wise cube of a matrix cube = M1 .^ 3 % element wise reciprocal reciprocal = 1 ./ M1 % element wise logarithmic logarithmic = log(M3) % element wise exponent exponent = exp(M3) % fetching the element wise absolute value absolute = abs([-1 -2; -3 -4; -5 -6]) % initializing a vector vec = [1 2 3 4 5]; % element wise multiply with -1 additive_inverse = -vec % similar to vec * -1 % adding 1 to every element add_1 = vec + 1 % transpose of a matrix transpose = M1' % getting the maximum value maximum = max(vec) % getting the maximum value with index [value, index] = max(vec) % getting column wise maximum value col_max = max(M1) % index of elements that satisfies a condition index = find(vec > 3)
Producción :
mat_mul = 330 396 462 726 891 1056 1122 1386 1650 ele_mul = 11 44 99 176 275 396 539 704 891 cube = 1 8 27 64 125 216 343 512 729 reciprocal = 1.00000 0.50000 0.33333 0.25000 0.20000 0.16667 0.14286 0.12500 0.11111 logarithmic = 0.00000 0.69315 0.00000 0.69315 exponent = 2.7183 7.3891 2.7183 7.3891 absolute = 1 2 3 4 5 6 additive_inverse = -1 -2 -3 -4 -5 add_1 = 2 3 4 5 6 transpose = 1 4 7 2 5 8 3 6 9 maximum = 5 value = 5 index = 5 col_max = 7 8 9 index = 4 5
2. Array mágica: una array mágica es una array en la que la suma de todas sus filas, columnas y diagonales es la misma. Usaremos la magic()
función para generar una array mágica.
% generating a 4x4 magic matrix magic_mat = magic(4) % fetching 2 column vectors corresponding % to row and column each which combination % shows you the element which are greater then 10 in % our example such indexes are (1, 1), (2, 2), (4, 2) etc. [row, column] = find(magic_mat >= 10) % sum of all elements of the matrix sum = sum(sum(magic_mat)) % product of all elements of the matrix product = prod(prod(magic_mat))
Producción:
magic_mat = 16 2 3 13 5 11 10 8 9 7 6 12 4 14 15 1 row = 1 2 4 2 4 1 3 column = 1 2 2 3 3 4 4 sum = 136 product = 20922789888000
3. Algunas funciones y operaciones matriciales y vectoriales más:
% declaring the vector vec = [1 2 3 4 5]; % rounded down value of each element floor_val = floor(vec) % rounded up value of each element ceil_val = ceil(vec) % element wise max of 2 matrices maximum = max(rand(2), rand(2)) % generate a magic square magic_mat = magic(3) % declaring a matrix A = [10 22 34; 45 56 67; 74 81 90]; % generate a column vector of elements of A col_A = A(:) % overall maximum of a matrix, method 1 max_A = max(max(A)) % overall maximum of a matrix, method 2 max_A = max(A(:)) % column wise sum of a matrix sum_col = sum(magic_mat, 1) % row wise sum of a matrix sum_row = sum(magic_mat, 2) % sum of diagonal elements sum_diag = sum(sum(magic_mat .* eye(3))) % flipping the identity matrix flipud(eye(3)) % inverse of matrix with pinv() function inverse = pinv(magic_mat)
Producción :
floor_val = 1 2 3 4 5 ceil_val = 1 2 3 4 5 maximum = 0.72570 0.34334 0.81113 0.68197 magic_mat = 8 1 6 3 5 7 4 9 2 col_A = 10 45 74 22 56 81 34 67 90 max_A = 90 max_A = 90 sum_col = 15 15 15 sum_row = 15 15 15 sum_diag = 15 ans = Permutation Matrix 0 0 1 0 1 0 1 0 0 inverse = 0.147222 -0.144444 0.063889 -0.061111 0.022222 0.105556 -0.019444 0.188889 -0.102778
Publicación traducida automáticamente
Artículo escrito por dikshantmalidev y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA