How to Perform Computational Operations in Octave?
Last Updated :
02 Feb, 2023
In this article, we will see how to perform some basic computational operations in Octave. Below is the list of various computational operations one can perform in Octave to use them in various machine learning algorithms : 1. Matrix Operations : Matrices are the core components of Octave. Let us see some matrix operations in Octave :
MATLAB
M1 = [1 2 3; 4 5 6; 7 8 9];
M2 = [11 22 33; 44 55 66; 77 88 99];
M3 = [1 2; 1 2];
mat_mul = M1 * M2
ele_mul = M1 .* M2
cube = M1 .^ 3
reciprocal = 1 ./ M1
logarithmic = log(M3)
exponent = exp(M3)
absolute = abs([-1 -2; -3 -4; -5 -6])
vec = [1 2 3 4 5];
additive_inverse = -vec
add_1 = vec + 1
transpose = M1'
maximum = max(vec)
[value, index] = max(vec)
col_max = max(M1)
index = find(vec > 3)
|
Output :
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. Magic Matrix : A magic matrix is a matrix in which the sum of all it’s rows, column, and diagonal is the same. We will use the magic() function to generate a magic matrix.
MATLAB
magic_mat = magic(4)
[row, column] = find(magic_mat >= 10)
sum = sum(sum(magic_mat))
product = prod(prod(magic_mat))
|
Output:
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. Some more matrix and vector functions and operations :
MATLAB
vec = [1 2 3 4 5];
floor_val = floor(vec)
ceil_val = ceil(vec)
maximum = max(rand(2), rand(2))
magic_mat = magic(3)
A = [10 22 34; 45 56 67; 74 81 90];
col_A = A(:)
max_A = max(max(A))
max_A = max(A(:))
sum_col = sum(magic_mat, 1)
sum_row = sum(magic_mat, 2)
sum_diag = sum(sum(magic_mat .* eye(3)))
flipud(eye(3))
inverse = pinv(magic_mat)
|
Output :
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...