0% found this document useful (0 votes)
15 views7 pages

Control Your Self Ni Jimwell

The document outlines various activities related to matrix creation and operations, including creating different sized matrices, performing addition, subtraction, multiplication, and element-wise division of matrices. It also includes examples of modifying matrix elements and generating random matrices. The activities are attributed to Jimwell A. Perez.

Uploaded by

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

Control Your Self Ni Jimwell

The document outlines various activities related to matrix creation and operations, including creating different sized matrices, performing addition, subtraction, multiplication, and element-wise division of matrices. It also includes examples of modifying matrix elements and generating random matrices. The activities are attributed to Jimwell A. Perez.

Uploaded by

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

IV: SOLUTION

ACTIVITY A: CREATING MATRICES


>> %Jimwell A. Perez
>> %Creating a 5x5 matrix (5 rows, 5 columns)
>> A=[11 12 13 14 15; 1 2 3 4 5; 2 4 6 8 10; 10 9 8 7 6; 3 6 9 12 15] ;
>> disp(A) ;
11 12 13 14 15
1 2 3 4 5
2 4 6 8 10
10 9 8 7 6
3 6 9 12 15

>> %Creating a 2x3 matrix with random integer in between 1 to 15 (2


rows, 3 columns)
>> A= randi ([1, 15], 2, 3) ;
>> disp(A) ;

5 15 3
9 15 15

>> %Creating a 5x1 matrixx containing numbers 1, 3, 5, 7, 9. ( 5 rows,


1 column)
>> A= [1; 3; 5; 7; 9] ;
>> disp(A) ;
1
3
5
7
9

>> %Creating a 10x9 matrix filled with zeros (10 rows, 9 columns)
>> A= [0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0,
0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0;
0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0,
0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0; 0, 0, 0, 0, 0, 0, 0, 0, 0] ;
>> disp(A) ;

0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0

>> %Creating a 5x5 matrix (5 rows, 5 columns)


>> A= diag([1, 2, 3, 4, 5]);
>> disp(A);

1 0 0 0 0
0 2 0 0 0
0 0 3 0 0
0 0 0 4 0
0 0 0 0 5
ACTIVITY 2: APPLYING MATRIX OPERATIONS
>> %Jimwell A. Perez
>> %Matrix Addition (Add Matrix A and B)
>> A= [3 6 9 12 11; 10 8 6 4 2; 11 13 15 17 19; 6 20 16 13 4; 21 23 25
27 29] ;
>> B= [5 7 9 11 13; 6 8 10 12 14; 18 14 1 5 22; 16 18 20 22 24; 26 27
28 31 33] ;
>> C= A + B;
>> disp(A);
3 6 9 12 11
10 8 6 4 2
11 13 15 17 19
6 20 16 13 4
21 23 25 27 29

>> disp(B);
5 7 9 11 13
6 8 10 12 14
18 14 1 5 22
16 18 20 22 24
26 27 28 31 33

>> disp(C);
8 13 18 23 24
16 16 16 16 16
29 27 16 22 41
22 38 36 35 28
47 50 53 58 62

>> %Matrix Subtraction(Subtract Matrix B from A)


>> B= [5 7 9 11 13; 6 8 10 12 14; 18 14 1 5 22; 16 18 20 22 24; 26 27
28 31 33]
>> A= [3 6 9 12 11; 10 8 6 4 2; 11 13 15 17 19; 6 20 16 13 4; 21 23 25
27 29]
>> C= B - A;
>> disp(C);

B =

5 7 9 11 13
6 8 10 12 14
18 14 1 5 22
16 18 20 22 24
26 27 28 31 33

A =

3 6 9 12 11
10 8 6 4 2
11 13 15 17 19
6 20 16 13 4
21 23 25 27 29
C=
2 1 0 -1 2
-4 0 4 8 12
7 1 -14 -12 3
10 -2 4 9 20
5 4 3 4 4
>> %Matrix Multiplication (Multiply Matrix A and B)
>>A= [3 6 9 12 11; 10 8 6 4 2; 11 13 15 17 19; 6 20 16 13 4; 21 23 25
27 29]
>>B= [5 7 9 11 13; 6 8 10 12 14; 18 14 1 5 22; 16 18 20 22 24; 26 27 28
31 33]
>>C= A*B;
>>disp©;

A =

3 6 9 12 11
10 8 6 4 2
11 13 15 17 19
6 20 16 13 4
21 23 25 27 29

B =

5 7 9 11 13
6 8 10 12 14
18 14 1 5 22
16 18 20 22 24
26 27 28 31 33
C=
691 708 644 755 972
322 344 312 386 536
1169 1210 1116 1315 1690
750 768 642 796 1154
1879 1950 1796 2125 2750

>> %Elements-wise Division of Matrix A by Matrix B


>> A= [3 6 9 12 11; 10 8 6 4 2; 11 13 15 17 19; 6 20 16 13 4; 21 23 25
27 29]
>> B= [5 7 9 11 13; 6 8 10 12 14; 18 14 1 5 22; 16 18 20 22 24; 26 27
28 31 33]
>> C= A./B;
>> disp©;

A =

3 6 9 12 11
10 8 6 4 2
11 13 15 17 19
6 20 16 13 4
21 23 25 27 29

B =

5 7 9 11 13
6 8 10 12 14
18 14 1 5 22
16 18 20 22 24
26 27 28 31 33
C=
0.6000 0.8571 1.0000 1.0909 0.8462
1.6667 1.0000 0.6000 0.3333 0.1429
0.6111 0.9286 15.0000 3.4000 0.8636
0.3750 1.1111 0.8000 0.5909 0.1667
0.8077 0.8519 0.8929 0.8710 0.8788

ACTIVITY 3. Creating a Matrix (Circle Your Answer)


>> %Jimwell A. Perez
>> %Creating a 10x10 matrix with random integer in between 1 to 100 (10
rows, 10 columns)
>> A= randi ([1, 100], 10, 10);
>> disp(A);
96 85 28 77 66 51 82 59 54 32
49 94 5 80 17 70 25 55 78 53
81 68 10 19 12 90 93 92 94 17
15 76 83 49 50 96 35 29 13 61
43 75 70 45 96 55 20 76 57 27
92 40 32 65 35 14 26 76 47 66
80 66 96 71 59 15 62 39 2 69
96 18 4 76 23 26 48 57 34 75
66 71 44 28 76 85 36 8 17 46
4 4 39 68 26 26 84 6 80 9

>> %Modify the element (position (8,8)) with value of 12


>> A(8,8)= 12; %It will change the element at row 8, column 8 to 12
>> disp(A);
96 85 28 77 66 51 82 59 54 32
49 94 5 80 17 70 25 55 78 53
81 68 10 19 12 90 93 92 94 17
15 76 83 49 50 96 35 29 13 61
43 75 70 45 96 55 20 76 57 27
92 40 32 65 35 14 26 76 47 66
80 66 96 71 59 15 62 39 2 69
96 18 4 76 23 26 48 12 34 75
66 71 44 28 76 85 36 8 17 46
4 4 39 68 26 26 84 6 80 9

>> %Modify the element position of fifth row and seventh column with
the month of your birthday
>> A(5,:)= [5]; %Modify the entire 5th row
>> A(:,7)= [5]; %Modify the entire 7th column
>> disp(A);

96 85 28 77 66 51 5 59 54 32
49 94 5 80 17 70 5 55 78 53
81 68 10 19 12 90 5 92 94 17
15 76 83 49 50 96 5 29 13 61
5 5 5 5 5 5 5 5 5 5
92 40 32 65 35 14 5 76 47 66
80 66 96 71 59 15 5 39 2 69
96 18 4 76 23 26 5 12 34 75
66 71 44 28 76 85 5 8 17 46
4 4 39 68 26 26 5 6 80 9
>> %Modify the element position of tenth row and tenth column with the
value of 1
>> A(10,:)= [1]; %Modify the entire 10th row
>> A(:,10)= [1]; %Modify the entire 10th column
>> disp(A);

96 85 28 77 66 51 5 59 54 1
49 94 5 80 17 70 5 55 78 1
81 68 10 19 12 90 5 92 94 1
15 76 83 49 50 96 5 29 13 1
5 5 5 5 5 5 5 5 5 1
92 40 32 65 35 14 5 76 47 1
80 66 96 71 59 15 5 39 2 1
96 18 4 76 23 26 5 12 34 1
66 71 44 28 76 85 5 8 17 1
1 1 1 1 1 1 1 1 1 1

>> %Modify the second row with your highest-grade last semester
>> A(2,:)= [85]; %Modify the entire 2nd row
>> disp(A);
96 85 28 77 66 51 5 59 54 1
85 85 85 85 85 85 85 85 85 85
81 68 10 19 12 90 5 92 94 1
15 76 83 49 50 96 5 29 13 1
5 5 5 5 5 5 5 5 5 1
92 40 32 65 35 14 5 76 47 1
80 66 96 71 59 15 5 39 2 1
96 18 4 76 23 26 5 12 34 1
66 71 44 28 76 85 5 8 17 1
1 1 1 1 1 1 1 1 1 1

>> %Modify the sixth column with your age


>> A(:,6)= [20]; %Modify the entire 6th column
>> disp(A);
96 85 28 77 66 20 5 59 54 1
85 85 85 85 85 20 85 85 85 85
81 68 10 19 12 20 5 92 94 1
15 76 83 49 50 20 5 29 13 1
5 5 5 5 5 20 5 5 5 1
92 40 32 65 35 20 5 76 47 1
80 66 96 71 59 20 5 39 2 1
96 18 4 76 23 20 5 12 34 1
66 71 44 28 76 20 5 8 17 1
1 1 1 1 1 20 1 1 1 1

>> %Replace the 3rd column with the values 1 to 10.


>> A (:,3)=[1; 2; 3; 4; 5; 6; 7; 8; 9; 10]; %Modify the entire 3rd
column
>> disp(A);
96 85 1 77 66 20 5 59 54 1
85 85 2 85 85 20 85 85 85 85
81 68 3 19 12 20 5 92 94 1
15 76 4 49 50 20 5 29 13 1
5 5 5 5 5 20 5 5 5 1
92 40 6 65 35 20 5 76 47 1
80 66 7 71 59 20 5 39 2 1
96 18 8 76 23 20 5 12 34 1
66 71 9 28 76 20 5 8 17 1
1 1 10 1 1 20 1 1 1 1

>> %Modify all the diagonal elements with the value of 24.
>> A(1:size (A,1) + 1: end)= 24;
>> disp(A);
24 85 1 77 66 20 5 59 54 1
85 24 2 85 85 20 85 85 85 85
81 68 24 19 12 20 5 92 94 1
15 76 4 24 50 20 5 29 13 1
5 5 5 5 24 20 5 5 5 1
92 40 6 65 35 24 5 76 47 1
80 66 7 71 59 20 24 39 2 1
96 18 8 76 23 20 5 24 34 1
66 71 9 28 76 20 5 8 24 1
1 1 10 1 1 20 1 1 1 24

You might also like