Experiment 1
Basics of Matrix Operation and Matrix Manipulation.
Program Code:
1. To make a 2D matrix or an array :
>> a = [10 20 30 ;40 50 60;70 80 90]
a=
10 20 30
40 50 60
70 80 90
>> matrix = [11 22 33;44 55 66;77 88 99]
matrix =
11 22 33
44 55 66
77 88 99
>> b = [1 2 3; 4 5 6; 7 8 9 ]
b=
1 2 3
4 5 6
7 8 9
2. To do matrix addition between two 2D matrices
>> c = a + b
c=
11 22 33
44 55 66
77 88 99
3. To do matrix subtraction between two 2D matrices
>> f=a-b
f=
9 18 27
36 45 54
63 72 81
4. To do matrix multiplication between two 2D matrices
>>g=a*b
g=
100 120 140
250 300 350
400 480 560
5. To do element wise multiplication of two 2D matrices
>> c =[1 2 3; 4 5 6; 7 8 9]
C=
1 2 3
4 5 6
7 8 9
>> g = matrix.*c
g=
11 44 99
176 275 330
539 704 720
6. To divide two 2D matrices
>> h = matrix./c
h=
11.0000 11.0000 11.0000
11.0000 11.0000 11.0000
11.0000 11.0000 11.0000
7. To do element wise exponential of two 2D matrices
>> i = matrix.^c
i=
1.0e+02 1.0e+05 1.0e+07
1.0e+07 1.0e+09 1.0e+11
1.0e+10 1.0e+12 1.0e+14
8. To make a zero matrix of 3x3
>> l = zeros(3)
l=
0 0 0
0 0 0
0 0 0
9. To make a matrix of ones of 3x3
>> k = ones(3)
k=
1 1 1
1 1 1
1 1 1
10. To make an identity matrix of 3x3
>> m = eye(3)
m=
1 0 0
0 1 0
0 0 1
11. To make a matrix of 50 equally spaced values between 1 and 100
>> n = linspace(10, 200, 50)
n=
Columns 1 through 8
10.0000 14.0816 18.1633 22.2449 26.3265 30.4082 34.4898 38.5714
Columns 9 through 16
42.6531 46.7347 50.8163 54.8980 58.9796 63.0612 67.1429 71.2245
Columns 17 through 24
75.3061 79.3878 83.4694 87.5510 91.6327 95.7143 99.7959 103.8776
Columns 25 through 32
107.9592 112.0408 116.1224 120.2041 124.2857 128.3673 132.4490 136.5306
Columns 33 through 40
140.6122 144.6939 148.7755 152.8571 156.9388 161.0204 165.1020 169.1837
Columns 41 through 48
173.2653 177.3469 181.4286 185.5102 189.5918 193.6735 197.7551 200.0000
Columns 49 through 50
200.0000 200.0000
12. To make a matrix with all numbers between 1 and 100 with a spacing of four
>> o = 5:10:200
o=
5 15 25 35 45 55 65 75 85 95 105 115 125 135 145 155 165 175
185 195
13. To replace an element in a matrix
>> matrix = [1 10 20; 30 40 50; 60 70 80]
matrix =
1 10 20
30 40 50
60 70 80
>> b = matrix(2, 3)
b=
50
14. To print either a column or row of a matrix
>> d = matrix(:, 2)
d=
10
40
70
>> c = matrix(1, :)
c=
1 10 20
15. To print specific rows of a matrix
>> e = matrix(2:3, :)
e=
30 40 50
60 70 80
16. To print specific columns of a matrix
>> f = matrix(:, 2:3)
f=
10 20
40 50
70 80
17. To print specific rows and columns of a matrix
>> g = matrix(1:2, 2:3)
g=
10 20
40 50
18. To print end column of a matrix
>> h = matrix(:, end)
h=
20
50
80
19. To reshape a matrix into its multiples (eg. 3x3 resized into 1 x 9)
>> i = reshape(matrix, 1, 9)
i=
1 10 20 30 40 50 60 70 80
>> j = reshape(matrix, 9, 1)
j=
1
10
20
30
40
50
60
70
80
20. To flip a matrix upside down
>> k = flipud(matrix)
k=
60 70 80
30 40 50
1 10 20
21. To flip a matrix from left to right
>> l = fliplr(matrix)
l=
20 10 1
50 40 30
80 70 60
22. To replace a whole column of a matrix
>> matrix(:,2) = [23; 42; 53]
matrix =
1 23 20
30 42 50
60 53 80
23. To replace a whole row of a matrix
>> matrix (1, :) = [9 9 9]
matrix =
9 9 9
30 42 50
60 53 80
24. To replace a specific columns of a matrix
>> matrix(:, 2:3) = [4 2; 45 5; 6 12]
matrix =
9 4 2
30 45 5
60 6 12
25. To replace a specific rows and columns of a matrix
>> matrix(1:2, 2:3) = [4 4; 23 23]
matrix =
9 4 4
30 23 23
60 6 12