Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB Replace. Arrange Quick } . Styles. ExE(4) a) Create B which is a 4 by 6 m

ID: 3597266 • Letter: M

Question

MATLAB

Replace. Arrange Quick } . Styles. ExE(4) a) Create B which is a 4 by 6 matrix with elements evenly spaced between -20 and 18. b) Create another matrix C that is 3 by 8 with random integers between -30 and 8. c) Try to add the elements in B with elements in C and store the 24 numbers in a row vector h. d) Flip the columns of B to the right and store the results in A. e) Subtract A from B and report the results. f) Create a new matrix G which has 6 rows and 32 columns and has the same elements as C g) Multiply B and G and store the results in K. h) How many elements K has? What is the size of K? 10/20/2017

Explanation / Answer

clc
clear all

vec_B = linspace(-19, 17, 24) % creating the 24 elements as a row vector
% changing into matrix
B = [vec_B(1:4); vec_B(5:8); vec_B(9:12); vec_B(13:16); vec_B(17:20); vec_B(21:24)]

% creating the matrix C
C = randi([-29 7], 3, 8)
% temporaray variables h1 and h2 to make matrix B and C row vectors
h1 = [B(1, 1:4), B(2, 1:4), B(3, 1:4), B(4, 1:4), B(5, 1:4), B(6, 1:4)];
h2 = [C(1, 1:8), C(2, 1:8), C(3, 1:8)]

% adding the row vector obtained above and storing in h
h = h1 + h2
A = B'
% Making the dimension of matrix A and B equal by concatenating 0, to subtract them
A(6, 6) = 0
B(1, 6) = 0
sub_A_B = B - A

G = C;
% Padding 0 to matrix G to make it of 6 x 32 dimension
G(6, 32) = 0
K = B*G

% Number of elemnts in K
Num_elements_K = numel(K)

% Size of K
[K_row, K_column] = size(K)


vec_B =

Columns 1 through 9

-19.0000 -17.4348 -15.8696 -14.3043 -12.7391 -11.1739 -9.6087 -8.0435 -6.4783

Columns 10 through 18

-4.9130 -3.3478 -1.7826 -0.2174 1.3478 2.9130 4.4783 6.0435 7.6087

Columns 19 through 24

9.1739 10.7391 12.3043 13.8696 15.4348 17.0000


B =

-19.0000 -17.4348 -15.8696 -14.3043
-12.7391 -11.1739 -9.6087 -8.0435
-6.4783 -4.9130 -3.3478 -1.7826
-0.2174 1.3478 2.9130 4.4783
6.0435 7.6087 9.1739 10.7391
12.3043 13.8696 15.4348 17.0000


C =

-25 -7 -4 -5 -23 -11 -19 -14
0 6 -1 -25 -20 -1 -28 -13
-19 -13 -13 5 0 -15 -5 -7


h2 =

Columns 1 through 16

-25 -7 -4 -5 -23 -11 -19 -14 0 6 -1 -25 -20 -1 -28 -13

Columns 17 through 24

-19 -13 -13 5 0 -15 -5 -7


h =

Columns 1 through 9

-44.0000 -24.4348 -19.8696 -19.3043 -35.7391 -22.1739 -28.6087 -22.0435 -6.4783

Columns 10 through 18

1.0870 -4.3478 -26.7826 -20.2174 0.3478 -25.0870 -8.5217 -12.9565 -5.3913

Columns 19 through 24

-3.8261 15.7391 12.3043 -1.1304 10.4348 10.0000


A =

-19.0000 -12.7391 -6.4783 -0.2174 6.0435 12.3043
-17.4348 -11.1739 -4.9130 1.3478 7.6087 13.8696
-15.8696 -9.6087 -3.3478 2.9130 9.1739 15.4348
-14.3043 -8.0435 -1.7826 4.4783 10.7391 17.0000


A =

-19.0000 -12.7391 -6.4783 -0.2174 6.0435 12.3043
-17.4348 -11.1739 -4.9130 1.3478 7.6087 13.8696
-15.8696 -9.6087 -3.3478 2.9130 9.1739 15.4348
-14.3043 -8.0435 -1.7826 4.4783 10.7391 17.0000
0 0 0 0 0 0
0 0 0 0 0 0


B =

-19.0000 -17.4348 -15.8696 -14.3043 0 0
-12.7391 -11.1739 -9.6087 -8.0435 0 0
-6.4783 -4.9130 -3.3478 -1.7826 0 0
-0.2174 1.3478 2.9130 4.4783 0 0
6.0435 7.6087 9.1739 10.7391 0 0
12.3043 13.8696 15.4348 17.0000 0 0


sub_A_B =

0 -4.6957 -9.3913 -14.0870 -6.0435 -12.3043
4.6957 0 -4.6957 -9.3913 -7.6087 -13.8696
9.3913 4.6957 0 -4.6957 -9.1739 -15.4348
14.0870 9.3913 4.6957 0 -10.7391 -17.0000
6.0435 7.6087 9.1739 10.7391 0 0
12.3043 13.8696 15.4348 17.0000 0 0


G =

Columns 1 through 15

-25 -7 -4 -5 -23 -11 -19 -14 0 0 0 0 0 0 0
0 6 -1 -25 -20 -1 -28 -13 0 0 0 0 0 0 0
-19 -13 -13 5 0 -15 -5 -7 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

Columns 16 through 30

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

Columns 31 through 32

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


K =

Columns 1 through 9

776.5217 234.6957 299.7391 451.5217 785.6957 464.4783 928.5217 603.7391 0
501.0435 147.0435 187.0435 295.0000 516.4783 295.4348 602.9565 390.8696 0
225.5652 59.3913 74.3478 138.4783 247.2609 126.3913 277.3913 178.0000 0
-49.9130 -28.2609 -38.3478 -18.0435 -21.9565 -42.6522 -48.1739 -34.8696 0
-325.3913 -115.9130 -151.0435 -174.5652 -291.1739 -211.6957 -373.7391 -247.7391 0
-600.8696 -203.5652 -263.7391 -331.0870 -560.3913 -380.7391 -699.3043 -460.6087 0

Columns 10 through 18

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

Columns 19 through 27

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

Columns 28 through 32

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


Num_elements_K =

192


K_row =

6


K_column =

32

>>

Matlab Code

clc
clear all

vec_B = linspace(-19, 17, 24) % creating the 24 elements as a row vector
% changing into matrix
B = [vec_B(1:4); vec_B(5:8); vec_B(9:12); vec_B(13:16); vec_B(17:20); vec_B(21:24)]

% creating the matrix C
C = randi([-29 7], 3, 8)
% temporaray variables h1 and h2 to make matrix B and C row vectors
h1 = [B(1, 1:4), B(2, 1:4), B(3, 1:4), B(4, 1:4), B(5, 1:4), B(6, 1:4)];
h2 = [C(1, 1:8), C(2, 1:8), C(3, 1:8)]

% adding the row vector obtained above and storing in h
h = h1 + h2
A = B'
% Making the dimension of matrix A and B equal by concatenating 0, to subtract them
A(6, 6) = 0
B(1, 6) = 0
sub_A_B = B - A

G = C;
% Padding 0 to matrix G to make it of 6 x 32 dimension
G(6, 32) = 0
K = B*G

% Number of elemnts in K
Num_elements_K = numel(K)

% Size of K
[K_row, K_column] = size(K)