0% found this document useful (0 votes)
13 views3 pages

MLB4 112327

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

MLB4 112327

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MATRICES AND VECTORS

In MATLAB, matrices and vectors are fundamental data structures. MATLAB (Matrix
Laboratory) is designed for matrix-based computations, making it essential to understand how
to create, manipulate, and operate on them.
4.1 Basics of Matrices and vectors
4.1.1 Creating Vectors
A vector is a one-dimensional array of numbers. It can be a row vector or a column vector.
Row Vector
A = [1, 2, 3, 4]; % Using square brackets with commas or spaces
B = [5 6 7 8]; % Spaces also work
Column Vector
C = [1; 2; 3; 4]; % Using semicolons to separate rows
Generating Vectors Automatically
x = 1:5; % Creates a row vector [1 2 3 4 5]
y = 1:2:10; % [Start:Step:End] -> [1 3 5 7 9]
z = linspace(0, 10, 5); % Generates 5 equally spaced points from 0 to 10

4.1.2 Creating Matrices


A matrix is a two-dimensional array of numbers.
Manual Entry
M = [1 2 3; 4 5 6; 7 8 9]; % 3x3 matrix
Predefined Matrices
Z = zeros(3,3); % 3x3 matrix of zeros
O = ones(2,4); % 2x4 matrix of ones
I = eye(4); % 4x4 identity matrix
R = rand(3,3); % 3x3 matrix with random values between 0 and 1

4.1.3 Accessing Elements


A = [10 20 30; 40 50 60; 70 80 90];
val = A(2,3); % Access element at row 2, column 3 -> 60
row1 = A(1,:); % Extracts first row -> [10 20 30]
col2 = A(:,2); % Extracts second column -> [20; 50; 80]
4.2. Matrix Operations
4.2.1 Basic Operations
A = [1 2; 3 4];
B = [5 6; 7 8];
C = A + B; % Matrix addition
D = A - B; % Matrix subtraction
E = A .* B; % Element-wise multiplication
F = A ./ B; % Element-wise division

4.2.2 Matrix Multiplication


G = A * B; % Standard matrix multiplication (dot product)
To perform element-wise multiplication, use .*:
H = A .* B; % Element-wise multiplication

4.2.3 Transpose and Inverse


T = A'; % Transpose of A
invA = inv(A); % Inverse of A (if A is square and non-singular)
detA = det(A); % Determinant of A

4.3.Matrix Computations and Applications


4.3.1 Eigenvalues and Eigenvectors
[V,D] = eig(A); % V contains eigenvectors, D contains eigenvalues

4.3.2 Solving Linear Systems


Given Ax = b, solve for x:
A = [2 3; 1 4];
b = [5; 6];
x = A\b; % MATLAB’s efficient solver for Ax = b

Summary Table
Concept Example
Create Vector v = [1 2 3]
Create Matrix M = [1 2; 3 4]
Access Elements M(2,1)
Matrix Addition A+B
Matrix Multiplication A*B
Element-wise Multiplication A .* B
Transpose A'
Inverse inv(A)
Solve Linear System A\b
Eigenvalues & Vectors [V,D] = eig(A)

You might also like