MATLAB Programming 5 Create a 4x4 matrixG of random integers
MATLAB Programming:
5. Create a 4x4 matrix(G) of random integers between 1 and 30 using the command: G = randi(30, 4, 4)
(a) Find the elements in G whose values are greater than 12
(b) Find the sum of all the values in G
(c) Find the sum of all the values in the first row
(d) Find the sum of all the values in the second row
Solution
G = randi(30,4,4);
disp(G);%displaying G
sum1 = 0;
disp(\'Values Greater than 12\');
for i=1:16
if G(i)>12
disp(G(i));%displaying the values which are greater than 12
end
sum1 = sum1+G(i);
end
disp(\'Sum of all values\');
disp(sum1);%displaying the sum
Gtranspose = transpose(G);%taking the transpose of the matrix
sumrow = sum(Gtranspose);%gives the sum of all values in each colums of transposed matrix which equal to sum of all rows in given matrix
fprintf(\'Sum of all values in first row %d\ \',sumrow(1));%displaying the sum of first row
fprintf(\'Sum of all values in second row %d\ \',sumrow(2));%displaying the sum of second row.
Output:
22 20 29 23
23 5 11 8
9 4 18 16
21 15 7 21
Values Greater than 12
22
23
21
20
15
29
18
23
16
21
Sum of all values
252
Sum of all values in first row 94
Sum of all values in second row 47
>>

