ARRAYS AND MATRICES
CONTINUED
eye (m,n) returns m by n with one on the
main diagonal.
UNIT 1
>>eye(3,4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
rand (m,n) returns m by n matrix of
random numbers. >>rand (2,3)
ans =
0.9501 0.6068 0.8913
0.2311 0.4860 0.7621
>>rand (2,3)
UNIT 1
ans =
0.4565 0.8214 0.6154
0.0185 0.4447 0.7919
>>rand (2,3)
ans =
0.9218 0.1763 0.9355
0.7382 0.4057 0.9169
>>randn (2,3) returns m by n matrix
of normally distributed numbers.
ans =
-0.4326 0.1253 -1.1465
-1.6656 0.2877 1.1909
>> x=0:pi/4:2*pi
UNIT 1
x=
0 0.7854 1.5708 2.3562
3.1416 3.9270 4.7124 5.4978
6.2832
diag (x)generates a square matrix of zeros
with vector x on diagonal.
>>diag (x) generates a square matrix of
zeros with vector x on diagonal.
ans =
0 0 0 0 0 0 0 0 0
0 0.7854 0 0 0 0 0 0 0
0 0 1.5708 0 0 0 0 0 0
UNIT 1
0 0 0 2.3562 0 0 0 0 0
0 0 0 0 3.1416 0 0 0 0
0 0 0 0 0 3.9270 0 0 0
0 0 0 0 0 0 4.7124 0 0
0 0 0 0 0 0 0 5.4978 0
0 0 0 0 0 0 0 0
6.2832
diag (A) extracts diagonal of matrix a as
vectors.
>> A= [2 3 4 2.3 7;3 4 5 6 7;8 9 10 11 12]
A=
2.0000 3.0000 4.0000 2.3000 7.0000
3.0000 4.0000 5.0000 6.0000 7.0000
UNIT 1
8.0000 9.0000 10.0000 11.0000 12.0000
>>diag (A)
ans =
2
4
10
diag (A,2) extracts the second upper
off diagonal vector of matrix A.
>>diag (A,2)
ans =
4
6
UNIT 1
12
rot90 rotates a matrix by 900
rot90(A,K) is the K*90 degree rotation
of A, K = +-1,+-2,... (anti clockwise)
flipr flips a matrix from left to
right.
flipud flips a matrix from up to down.
tril extracts the lower triangular part of
trilu extracts the upper triangular part of
the matrix.
reshape changes the shape of a matrix.
>> c=[2.0000 7.0000 7.0000 6.0000 2.6700
11.0000 6.0000 2.3000 5.0000 54.0000
10.0000 5.0000 4.0000 4.0000 4.0000
UNIT 1
9.0000 4.0000 3.0000 2.3000 12.0000];
>> c=[2.0000 7.0000 7.0000 6.0000 2.6700
11.0000 6.0000 2.3000 5.0000 54.0000
10.0000 5.0000 4.0000 4.0000 4.0000
9.0000 4.0000 3.0000 2.3000 12.0000];
>> reshape (c, 5,4)
ans =
2.0000 6.0000 4.0000 2.3000
11.0000 5.0000 3.0000 2.6700
10.0000 4.0000 6.0000 54.0000
9.0000 7.0000 5.0000 4.0000
UNIT 1
7.0000 2.3000 4.0000 12.0000
m,n]=size (c)
m=
4
n=
5
>>size (c)
ans =
5 5
length(X) returns the length of vector X.
It is equivalent to MAX(SIZE(X)) for non-
empty arrays and 0 for empty ones.
>> d=[1 2;3.5 4.3];
UNIT 1
>> d+6
ans =
7.0000 8.0000
9.5000 10.3000
>> 2*d
ans =
2.0000 4.0000
7.0000 8.6000
>> d^2
ans =
8.0000 10.6000
18.5500 25.4900
[1*1+2*3.5 =8 1*2+2*4.3=10.6
UNIT 1
3.5*1+4.3*3.5=18.55 3.5*2+4.3*4.3=25.49];
>> d-1
ans =
0 1.0000
2.5000 3.3000
e=[1 2;3 4];
>> f=[2 4;9 16];
>> e.^2
ans =
1 4
9 16
UNIT 1
>> sqrt(f)
ans =
1.4142 2.0000
3.0000 4.0000
>> sqrt_m=sqrtm(f)
sqrt_m =
0.5138 + 0.4122i 0.9260 - 0.1016i
2.0834 - 0.2287i 3.7546 + 0.0564i
>> sqrt_m^2
ans =
2.0000 - 0.0000i 4.0000 - 0.0000i
9.0000 + 0.0000i 16.0000 + 0.0000i
>>f=[log(1) log(2);log(3) log(4)];
>> exp(f)
UNIT 1
ans =
1.0000 2.0000
3.0000 4.0000
>> g=[1 10;100 1000];
>> log10(g)
ans =
0 1
2 3
>> e=[1 2;3 4];
>> p=exp(e)
p=
2.7183 7.3891
20.0855 54.5982
>> log(p)
UNIT 1
ans =
1 2
3 4
>> q= expm(e)
q=
51.9690 74.7366
112.1048 164.0738
>> logm(q)
ans =
1.0000 2.0000
3.0000 4.0000
a=[1 3 9
5 10 15
UNIT 1
0 0 -5];
>> a(2,3)
ans =
15
>> a(3,1:4)
??? Index exceeds matrix dimensions.
>> a(3,1:3)
ans =
0 0 -5
If A is a 10x10 matrix B is a 5x10 matrix
and Y is a 20 element long row vector,
UNIT 1
then
>> A([1 3 6 9],:) = [B(1:3,:);Y(6:15)]
replaces 1st 3rd 6th row of A by the
fist third row of B and the 9th row of
A by the 6to 15 element of Y.
>> Q=[ 2 3 6 0 5; 0 0 20 -4 3;1 2 3 9 8 ...
; 2 -5 5 -5 6; 5 10 15 20 25];
>> v=[1 4 5];
>>Q(v,:)
ans = 2 3 6 0 5
2 -5 5 -5 6
UNIT 1
5 10 15 20 25
>>Q(:,v)
ans =
2 0 5
0 -4 3
1 9 8
2 -5 6
5 20 25
>> B=Q(:)
B=
2
0
1
2
UNIT 1
5
3
0
2
-5
10
6
20
3
5
15
0
-4
9
UNIT 1
-5
20
5
3
8
6
25
>> u1=[1 2 3 4 5 2 3 4 5 5 23 12];
>>u1(3:length(u1))=[]
u1 =
1 2
>>whos
Name Size Bytes Class
UNIT 1
B 1x25 200 double array
Q 5x5 200 double array
a 3x3 72 double array
ans 3x5 120 double array
s 1x6 6 logical array
u1 1x2 16 double array
v 1x3 3 logical array
Grand total is 85 elements using 617 bytes
>>who
Your variables are:
B Q a ans s u1 v
>> d1=[2 4 6 8];
>> d2=[-3 -3 -3];
UNIT 1
>> d3=[-1 -1];
>> D=diag(d1)+diag (d2,1)+diag(d3,-2)
D=
2 -3 0 0
0 4 -3 0
-1 0 6 -3
0 -1 0 8
Workspace
>>diary 'mah'
>> d=[ 4.3 2.3 3.2 1.4];
>> a=complex(d,d)
a=
UNIT 1
4.3000 + 4.3000i 2.3000 + 2.3000i
3.2000 + 3.2000i 1.4000 + 1.4000i
>>a'
ans =
4.3000 - 4.3000i
2.3000 - 2.3000i
3.2000 - 3.2000i
1.4000 - 1.4000i
>> save a,d
UNIT 1
d=
4.3000 2.3000 3.2000 1.4000
>> save ans
>> save a d
>>diary off
Note diary can be accessed by double
clicking on the file name of diary shown in
the working folder.
Output of diary mah
d=[ 4.3 2.3 3.2 1.4];
UNIT 1
a=complex(d,d)
a=
4.3000 + 4.3000i 2.3000 + 2.3000i
3.2000 + 3.2000i 1.4000 + 1.4000i
a’
ans =
4.3000 - 4.3000i
2.3000 - 2.3000i
UNIT 1
3.2000 - 3.2000i
1.4000 - 1.4000i
save a,d
Cell indexing and generation
>>sample= cell(2,2)
sample =
[] []
UNIT 1
[] []
>>sample(1,1)={[1 2 3; 4 5 6; 7 8 9]}
sample =
[3x3 double] []
[] []
>>celldisp(sample)
sample{1,1} =
1 2 3
4 5 6
7 8 9
sample{2,1} =
UNIT 1
[]
sample{1,2} =
[]
sample{2,2} =
[]
>>sample(2,2)={'Matlab'};
>>sample(2,1)={[ 2i 1-7i -6]};
>>sample(1 2)={[ 'abcd' 'efgh' 'ijkl']};
sample(1 2)={[ 'abcd' 'efgh' 'ijkl']};
Error: Unexpected MATLAB expression.
>>sample(1, 2)={[ 'abcd', 'efgh' ,'ijkl']};
>>sample
sample =
UNIT 1
[3x3 double] 'abcdefghijkl'
[1x3 double] 'Matlab‘
>>sample(1,1)
ans =
[3x3 double]
>>sample{1,1}
ans =
1 2 3
4 5 6
7 8 9
>>sample{2,1}
ans =
UNIT 1
0 + 2.0000i 1.0000 - 7.0000i -
6.0000
myNum = [1, 2, 3];
>>myNum = [1, 2, 3];
>>myCell = {'one', 'two'};
myStruct.Field1 = ones(3);
myStruct.Field2 = 5*ones(5);
C = {myNum, 100*myNum;
myCell, myStruct};
>>C{2,1}
ans =
UNIT 1
'one' 'two'
>>C{1,2}
ans =
100 200 300
>>C{2,2}
ans =
Field1: [3x3 double]
Field2: [5x5 double]
>>C{2,2}(1,1)
ans =
Field1: [3x3 double]
Field2: [5x5 double]
>>myNum = [1, 2, 3];
>>myCell = {'one', 'two'};
UNIT 1
>> myStruct.Field1 = ones(3);
>> myStruct.Field2 = 5*ones(5);
>> C = {myNum, 100*myNum;
myCell, myStruct};
>> C
C=
[1x3 double] [1x3 double]
{1x2 cell } [1x1 struct]
>>C{2,2}
ans =
Field1: [3x3 double]
Field2: [5x5 double]
>>C{2,2}(1,2)
UNIT 1
Index exceeds matrix dimensions.
>>C{2,2}(2,1)
Index exceeds matrix dimensions.
>>C{2,2}(1,1)
ans =
Field1: [3x3 double]
Field2: [5x5 double]
>>C{2,2}.Field1
ans =
1 1 1
1 1 1
1 1 1
>>C{2,2}.Field2
UNIT 1
ans =
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
5 5 5 5 5
>>C{2,2}.Field2(5,4)=3
C=
[1x3 double] [1x3 double]
{1x2 cell } [1x1 struct]
Content indexing
>>a{1,1}= [10 20 30; 3 40 50];
UNIT 1
>>a{1,2}=' Scientists';
>>a{2,1}= 1-7i;
>>a{2,2}=[' asd' 5 'fgh'];
>>a
a=
[2x3 double] ' Scientists'
[1.0000 - 7.0000i] ' asdfgh'
>>a{2,2} (1,2)
ans =
a
>>a{2,2} (1,1)
ans =
>>a{2,2} (1,5)
UNIT 1
ans =
>>a{2,2} (1,6)
ans =
f
>>a{2,2} (1,3:6)
ans =
sdf
Non Linear Equation
eval, feval
>>fx=inline('3*x+sin(x)-exp(x)')
fx =
Inline function:
fx(x) = 3*x+sin(x)-exp(x);
UNIT 1
>>dfx=inline('3+cos(x)-exp(x)')
dfx =
Inline function:
dfx(x) = 3+cos(x)-exp(x)
>>x0=2
x0 =
2
>>x1=x0-fx(x0)/dfx(x0)
x1 =
1.9002
>>x0=x1
x0 =
1.9002
UNIT 1
>>x1=x0-fx(x0)/dfx(x0)
x1 =
1.8901
>>x0=x1
x0 =
1.8901
>>x1=x0-fx(x0)/dfx(x0)
x1 =
1.8900
>>x0=x1
x0 =
1.8900
>>x1=x0-fx(x0)/dfx(x0)
UNIT 1
x1 =
1.8900
>> p3='x^3+x^2-3*x-3';
>> dp3=='3*x^2+2*x-3'
>> dp3='3*x^2+2*x-3'
dp3 =
3*x^2+2*x-3
>> x=2;
>> x=x-eval(p3)/eval(dp3)
x=
1.7692
>> x=x-eval(p3)/eval(dp3)
x=
1.7329
UNIT 1
>> x=x-eval(p3)/eval(dp3)
x=
1.7321
>> x=x-eval(p3)/eval(dp3)
x=
1.7321
>> x=x-eval(p3)/eval(dp3)
x= 1.7321
>> p4=inline(p3)
p4 =
Inline function:
p4(x) = x^3+x^2-3*x-3
>> x=2;
>>eval(p4)
UNIT 1
Undefined function 'eval' for input
arguments of type 'inline'.
Note: eval are not implemented on function
defined by inline command while feval are
for inline function not for the function
defined by string through quotes.
>> p4= inline('x^3+x^2-3*x-3');
>> dp4= inline('3*x^2+2*x-3');
>> x=2;
>> x=x-feval(p4,x)/feval(dp4,x)
x=
1.7692
UNIT 1
>> x=x-feval(p4,x)/feval(dp4,x)
x=
1.7329
>> x=x-feval(p4,x)/feval(dp4,x)
x=
1.7321
>> x=x-feval(p4,x)/feval(dp4,x)
x = 1.7321
function f =myfun (x)
f=[2*x(1)-x(2)-exp(-x(1));
-x(1) +2*x(2)-exp(-x(2))]
end
>> [x,fval]= fsolve (@myfun, [-5 -5])
UNIT 1
??? Error using ==>vertcat
All rows in the bracketed expression must
have the same
number of columns.
Error in ==>myfun at 2
f=[2*x(1)-x(2)-exp(-x(1));
Error in ==>fsolve at 180
fuser = feval(funfcn{3},x,varargin{:});
function f =myfun (x)
f=[(2*x(1)-x(2)-exp(-x(1)));
(-x(1) +2*x(2)-exp(-x(2)))]
end
>> [x,fval]= fsolve (@myfun, [-5 -5])
f=
UNIT 1
-153.4132
-153.4132
f=
-153.4131
-153.4132
f=
-153.4132
-153.4131
f=
-77.4708
-77.4708
f=
-77.4708
UNIT 1
-77.4708
f=
-77.4708
-77.4708
f=
-39.6675
-39.6675
f=
1.0e-006 *
-0.4208
-0.3677
Optimization terminated: first-order
optimality is less than options.TolFun.
UNIT 1
x=
0.5671 0.5671
fval =
1.0e-006 *
-0.4059
-0.4059
>>options= optimset('Display', 'iter');
>> [x,fval]= fsolve (@myfun, [-5 -5], options)
Norm of First-order Trust-region
Iteration Func-count f(x) step optimality radius
0 3 47071.2 2.29e+004 1
1 6 12003.4 1 5.75e+003 1
2 9 3147.02 1 1.47e+003 1
UNIT 1
3 12 854.452 1 388 1
4 15 239.527 1 107 1
5 18 67.0412 1 30.8 1
6 21 16.7042 1 9.05 1
7 24 2.42788 1 2.26 1
8 27 0.032658 0.759511 0.206 2.5
9 30 7.03149e-006 0.111927 0.00294 2.5
10 33 3.29525e-013 0.00169132 6.36e-007 2.5
Optimization terminated: first-order
optimality is less than options.TolFun.
x=
0.5671 0.5671
fval =
UNIT 1
1.0e-006 *
-0.4059
-0.4059
UNIT 1
end
UNIT 1
end
UNIT 1
end