ARRAY
Compiled by
Abu Raihan Ibna Ali (Pilu)
Abu.raihan.pilu@gmail.com
Student of Mechanical Engineering,CUET
Last modified date: 14-02-2018
ARRAY
An array is a list of numbers or data arranged in rows and on
columns.,
Array
Column
vector Row vector
Multi-
dimentional
array
1 2 3
4 5 6
7 8 9
Compiled by: Abu Raihan Ibna Ali (Pilu)
ARRAY
Row vector: In a row vector, the elements are entered
with a space or a coma between the elements inside
the square brackets
X= 1 2 3
Column vector: In a column vector, the elements are
entered with a semicolon between the elements inside
the square brackets.
X=
1
2
3
%Matlab Code
x=[1 2 3]
%Matlab Code
x=[1, 2, 3]
Or
%Matlab Code
x=[1;2;3]
Compiled by: Abu Raihan Ibna Ali (Pilu
ARRAY
Multi-dimensional array: Contains multiple rows and
columns
X=
%Matlab Code
x=[1 2 3;4 5 6;7 8 9]
%Matlab Code
x=[1,2,3;4,5,6;7,8,9]
Or
1 2 3
4 5 6
7 8 9
Compiled by: Abu Raihan Ibna Ali (Pilu)
ARRAY
Vector by ‘linspace’ command:
x=linspace(u,v,n)
Here, u=starting value
v=last value
n=total elements
%Matlab Code
x=linspace(1,20,5) Output
Compiled by: Abu Raihan Ibna Ali (Pilu)
ARRAY
Vector by colon:
x=[p:q:r]
Here, p=starting value
q= increment/ decrement
r=last value
%Matlab Code
x=[1:2:10]
Output
Compiled by: Abu Raihan Ibna Ali (Pilu)
ARRAY ADDRESSING
%Matlab Code
Output
v=[1:2:10]
v(:) %represents all the row or column elements of the vector v.
v(2:5) % represents the second through fth elements; that is v(2),
v(3),v(4), v(5).
Compiled by: Abu Raihan Ibna Ali (Pilu)
ARRAY ADDRESSING
%Matlab Code
Output
A=[2,4,10,13;16,3,7,18;8,4,9,25;3,12,15,17]
A(:,3) %denotes all the elements in
the third column of the matrix A.
A(3,:) %denotes all the elements in
the third row of A.
A(:,2:3) %denotes all the elements in
the second through third columns of
A.
A(2:3,1:3) %denotes all the elements
in the second and third rows that
are also in the first through third
columns.
v(2:5) % represents the second
through fth elements; that is v(2),
v(3),v(4), v(5).
%Output
Compiled by: Abu Raihan Ibna Ali (Pilu)
SOME FUNCTIONS OF ARRAY
find(X) Computes an array containing the indices of the nonzero elements of the
array x.
length(A) Computes either the number of elements of A if A is a vector or the largest
value of
m or n if A is an m # n matrix
linspace(a,b,n) Creates a row vector of n regularly spaced values between a and b
max(A) Returns the algebraically largest element in A if A is a vector. Returns a row
vector
containing the largest elements in each column if A is a matrix. If any of the
elements
are complex, max(A) returns the elements that have the largest magnitudes.
min(A) Same as max(A) but returns minimum values.
norm(x) Computes a vector’s geometric length 𝑥1
2
+ 𝑥2
2
+ ⋯ + 𝑥 𝑛
2
sum(A) Sums the elements in each column of the array A and returns a row vector
containing the sums
size(A) Returns a row vector [m n] containing the sizes of the m # n array A.
sort(A) Sorts each column of the array A in ascending order and returns an array
the same size as A
Compiled by: Abu Raihan Ibna Ali (Pilu)
EXAMPLE OF SOME FUNCTIONS OF ARRAY
%Matlab Code
%example 1
A=[2,4,0,13]
find(A)
length(A)
max(A)
min(A)
size(A)
sum(A)
% example 2
B=[2,1,4]
norm(B)
%Output
Compiled by: Abu Raihan Ibna Ali (Pilu)
SOME FUNCTIONS OF ARRAY
Command Description
eye(n) Creates an 𝑛 × 𝑛 identity matrix.
ones(n) Creates an n× 𝑛 matrix of 1s.
ones(m,n) Creates an m × n array of 1s.
zeros(n) Creates an n × n matrix of 0s
zeros(m,n) Creates an m × n array of 0s.
Compiled by: Abu Raihan Ibna Ali (Pilu)
SOME FUNCTIONS OF ARRAY
%Matlab Code
%example
a=eye(3)
b=ones(3)
c=ones(3,2)
d=zeros(3)
e=zeros(3,2)
%Output
Compiled by: Abu Raihan Ibna Ali (Pilu)
EXAMPLE OF VECTOR
%Output
Problem: A=60i – 25j –30k
B=30i – 55j – 30k
Find the magnitude of A, B and also find distance between A,B (AB).
A=[60,-25,-30];
B=[30,-55,-30];
magA=norm(A)
magB=norm(B)
AB=A-B
magAB=norm(AB)
Problem solution:
A=60i – 25j –30k
B=30i – 55j – 30k
a) 𝐴 = 602 + (−25)2+(−30)2
b) 𝐵 = 302 + (−55_2+(−30)2
c)AB= (30 − 60)2+(−55 + 25)2+(−20 + 30)2
%Code
Compiled by: Abu Raihan Ibna Ali (Pilu)
POLYNOMIAL OPERATIONS OF ARRAY
Find the roots of polynomial 3𝑥2+2x−2.
p = [3 2 -2]
roots(p)
%Output%Code
Find the value of polynomial 3𝑥2+2x−2. at x=2
x = [3 2 -2]
polyval(x,2)
%Output%Code
Compiled by: Abu Raihan Ibna Ali (Pilu)
SOLVING LINEAR EQUATION
Solve the following questions
A=[1,-1,3,5;
2,1,-1,1;
-1,-1,-1,5;
1,1,-1,5]
B=[7;6;5;4]
disp('Solutions are:')
X=inv(A)*B
%Output%Code
Compiled by: Abu Raihan Ibna Ali (Pilu)
ELEMENT WISE OPERATION OF MATRIX
A=
2 0 3
4 5 6
7 1 9
B=
1 2 3
4 5 6
7 8 9
A=[2,0,3;4,5,6;7,1,9]
B=[1,2,3;4,5,6;7,8,9]
Mat=A.*B
Mat1=A.^2
%Output%Code
Compiled by: Abu Raihan Ibna Ali (Pilu)
MATRIX OPERATONS
A=
2 0 3
4 5 6
7 1 9
B=
1 2 3
4 5 6
7 8 9
Find A+B, A-B, A*B
A=[2,0,3;4,5,6;7,1,9]
B=[1,2,3;4,5,6;7,8,9]
Mat=A+B
Mat1=A-B
Mat2=A*B
%Output%Code
For product,
Column of 1st matrix = row of 2nd
matrix
Compiled by: Abu Raihan Ibna Ali (Pilu)
THANK YOU
Compiled by: Abu Raihan Ibna Ali (Pilu)

Matrices, Arrays and Vectors in MATLAB

  • 1.
    ARRAY Compiled by Abu RaihanIbna Ali (Pilu) Abu.raihan.pilu@gmail.com Student of Mechanical Engineering,CUET Last modified date: 14-02-2018
  • 2.
    ARRAY An array isa list of numbers or data arranged in rows and on columns., Array Column vector Row vector Multi- dimentional array 1 2 3 4 5 6 7 8 9 Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 3.
    ARRAY Row vector: Ina row vector, the elements are entered with a space or a coma between the elements inside the square brackets X= 1 2 3 Column vector: In a column vector, the elements are entered with a semicolon between the elements inside the square brackets. X= 1 2 3 %Matlab Code x=[1 2 3] %Matlab Code x=[1, 2, 3] Or %Matlab Code x=[1;2;3] Compiled by: Abu Raihan Ibna Ali (Pilu
  • 4.
    ARRAY Multi-dimensional array: Containsmultiple rows and columns X= %Matlab Code x=[1 2 3;4 5 6;7 8 9] %Matlab Code x=[1,2,3;4,5,6;7,8,9] Or 1 2 3 4 5 6 7 8 9 Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 5.
    ARRAY Vector by ‘linspace’command: x=linspace(u,v,n) Here, u=starting value v=last value n=total elements %Matlab Code x=linspace(1,20,5) Output Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 6.
    ARRAY Vector by colon: x=[p:q:r] Here,p=starting value q= increment/ decrement r=last value %Matlab Code x=[1:2:10] Output Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 7.
    ARRAY ADDRESSING %Matlab Code Output v=[1:2:10] v(:)%represents all the row or column elements of the vector v. v(2:5) % represents the second through fth elements; that is v(2), v(3),v(4), v(5). Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 8.
    ARRAY ADDRESSING %Matlab Code Output A=[2,4,10,13;16,3,7,18;8,4,9,25;3,12,15,17] A(:,3)%denotes all the elements in the third column of the matrix A. A(3,:) %denotes all the elements in the third row of A. A(:,2:3) %denotes all the elements in the second through third columns of A. A(2:3,1:3) %denotes all the elements in the second and third rows that are also in the first through third columns. v(2:5) % represents the second through fth elements; that is v(2), v(3),v(4), v(5). %Output Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 9.
    SOME FUNCTIONS OFARRAY find(X) Computes an array containing the indices of the nonzero elements of the array x. length(A) Computes either the number of elements of A if A is a vector or the largest value of m or n if A is an m # n matrix linspace(a,b,n) Creates a row vector of n regularly spaced values between a and b max(A) Returns the algebraically largest element in A if A is a vector. Returns a row vector containing the largest elements in each column if A is a matrix. If any of the elements are complex, max(A) returns the elements that have the largest magnitudes. min(A) Same as max(A) but returns minimum values. norm(x) Computes a vector’s geometric length 𝑥1 2 + 𝑥2 2 + ⋯ + 𝑥 𝑛 2 sum(A) Sums the elements in each column of the array A and returns a row vector containing the sums size(A) Returns a row vector [m n] containing the sizes of the m # n array A. sort(A) Sorts each column of the array A in ascending order and returns an array the same size as A Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 10.
    EXAMPLE OF SOMEFUNCTIONS OF ARRAY %Matlab Code %example 1 A=[2,4,0,13] find(A) length(A) max(A) min(A) size(A) sum(A) % example 2 B=[2,1,4] norm(B) %Output Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 11.
    SOME FUNCTIONS OFARRAY Command Description eye(n) Creates an 𝑛 × 𝑛 identity matrix. ones(n) Creates an n× 𝑛 matrix of 1s. ones(m,n) Creates an m × n array of 1s. zeros(n) Creates an n × n matrix of 0s zeros(m,n) Creates an m × n array of 0s. Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 12.
    SOME FUNCTIONS OFARRAY %Matlab Code %example a=eye(3) b=ones(3) c=ones(3,2) d=zeros(3) e=zeros(3,2) %Output Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 13.
    EXAMPLE OF VECTOR %Output Problem:A=60i – 25j –30k B=30i – 55j – 30k Find the magnitude of A, B and also find distance between A,B (AB). A=[60,-25,-30]; B=[30,-55,-30]; magA=norm(A) magB=norm(B) AB=A-B magAB=norm(AB) Problem solution: A=60i – 25j –30k B=30i – 55j – 30k a) 𝐴 = 602 + (−25)2+(−30)2 b) 𝐵 = 302 + (−55_2+(−30)2 c)AB= (30 − 60)2+(−55 + 25)2+(−20 + 30)2 %Code Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 14.
    POLYNOMIAL OPERATIONS OFARRAY Find the roots of polynomial 3𝑥2+2x−2. p = [3 2 -2] roots(p) %Output%Code Find the value of polynomial 3𝑥2+2x−2. at x=2 x = [3 2 -2] polyval(x,2) %Output%Code Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 15.
    SOLVING LINEAR EQUATION Solvethe following questions A=[1,-1,3,5; 2,1,-1,1; -1,-1,-1,5; 1,1,-1,5] B=[7;6;5;4] disp('Solutions are:') X=inv(A)*B %Output%Code Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 16.
    ELEMENT WISE OPERATIONOF MATRIX A= 2 0 3 4 5 6 7 1 9 B= 1 2 3 4 5 6 7 8 9 A=[2,0,3;4,5,6;7,1,9] B=[1,2,3;4,5,6;7,8,9] Mat=A.*B Mat1=A.^2 %Output%Code Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 17.
    MATRIX OPERATONS A= 2 03 4 5 6 7 1 9 B= 1 2 3 4 5 6 7 8 9 Find A+B, A-B, A*B A=[2,0,3;4,5,6;7,1,9] B=[1,2,3;4,5,6;7,8,9] Mat=A+B Mat1=A-B Mat2=A*B %Output%Code For product, Column of 1st matrix = row of 2nd matrix Compiled by: Abu Raihan Ibna Ali (Pilu)
  • 18.
    THANK YOU Compiled by:Abu Raihan Ibna Ali (Pilu)