Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Execute MATLAB/Octave Online

% Line starting with "%" is called a comment in MATLAB, this line is ignored by the compiler. Almost all programming language has comment feature. In C, we give comments via "//" and in Python via "#".

% row matrix
% comma or space is used to separate elements of a row
r_matrix1 = [2 4 5 6 7]

r_matrix2 = [2, 4, 5, 6, 7]

% column matrix
% semicolon is used to separate rows
c_matrix = [2; 3; 4; 5; 7]

% 3*3 square matrix
sq_matrix = [1 2 3; 4 5 6; 7 8 9]

% diagonal matrix
diag_matrix1 = [1 0 0; 0 2 0; 0 0 3]

% D = diag(v) returns a square diagonal matrix with the elements of vector v on the main diagonal.
diag_matrix2 = diag(r_matrix1)

diag_matrix3 = diag([1,2,3])


%Addition, Subtraction, Multiplication 
m1 = [10 2 3; 14 5 6; 1 8 9]
m2 = [-11 7 8; 14 -5 6; 0 -1 9]

sum = m1 + m2
diff = m1 - m2

mul = m1 * m2

%Matrix divison in MATLAB has two types - right division, left divsion

%Matrix right division. B/A is roughly the same as B*inv(A). More precisely, B/A = (A'\B')'.
rdiv = m1/m2

%matrix left division. If A is a square matrix, A\B is roughly the same as inv(A)*B.
ldiv = m1\m2

%determinant
m = [ 1 2 3; 2 3 4; 1 2 5]
d = det(m)    

%transpose
mT = m'

%inverse
mI = inv(m)

%If the matrix is singular i.e. the determinant of the matrix is zero, then the inverse does not exist.

m2 = [0 0; 0 0]   % 2*2 matrix
d2 = det(m2)      % determinant will be 0
mI2 = inv(m2)     % inverse not possible, so you will warning by MATLAB coz of this 

%Eigenvalue
% e = eig(A) returns a column vector containing the eigenvalues of square matrix A.

e = eig(m)  % matrix m is defined earlier as [ 1 2 3; 2 3 4; 1 2 5]

%Operations
A = [1 2 3; 2 3 4; 1 2 5]
B = [-3 4 7; 5 8 -2; 0 1 3]

% X^p is X to the power p, if p is a scalar. If p is an integer, the power is computed by repeated squaring. You will learn about this method in Algorihtm course.

result1  = (A + B)^2     % could have done this also: (A + B) * (A + B)

result2  = A^2 + B^2     % could have done this also: A*A + B*B 

result3  = A' * B' 

result4  = B' * A

result5  = A^2 + B^2 - A*B + B'*A'

det_AB = det(A*B)

det_BA = det(B*A)







Advertisements
Loading...

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.