Define the array A = -4:3 Obtain array x by extracting the first 4 elements of t
ID: 3803007 • Letter: D
Question
Define the array A = -4:3 Obtain array x by extracting the first 4 elements of the array A Obtain array y by extracting the last 4 elements of the array A Obtain array z by stacking arrays x and y horizontally (this should be same as array A) Obtain array T by stacking arrays x and y vertically (x must be on top of y) Change x and y to column vectors and name them as P and Q Obtain array R by stacking arrays x and y horizontally Generate the following array using ones or zeros commands w = 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5Explanation / Answer
The solutions for the given questions are as follows with the matlab commands followed by their respective outputs and the corresponding matlab code is explained with comments followed by '%':-
A=-4:3; % Array a contains elements from -4 to 3
a) x=A(1:4) % x contains first four elements of array A
output:-
x =
-4 -3 -2 -1
b) y=A(5:8) % y contains last four elements of array A
output:-
y =
0 1 2 3
c) z(1:4)=x; % elements of x are stored in first four positions(indexes) of z
z(5:8)=y; % elements of y are stored in last four positions(indexes) of z
output:-
z =
-4 -3 -2 -1 0 1 2 3
d) T=z' % stacking arrays x and y vertically means transposing z
output:-
T =
-4
-3
-2
-1
0
1
2
3
e)
x=x'; % x is changed to column vector
y=y'; % y is changed to column vector
P=x % column vector x is stored in P
Q=y % column vector y is stored in P
output:-
P =
-4
-3
-2
-1
Q =
0
1
2
3
f) R=[x y] % stacking x and y (column vectors) horizantally
output:-
R =
-4 0
-3 1
-2 2
-1 3
5) w=5*ones(5,3) % ones(5,3) will create a matrix containing all elements as 1 with 5 rows and 3 columns
% 5 * ones(5,3) will multiply each element in the matrix ones(5,3) with 5 thus resulting in % required matrix.
% the resulting matrix will be stored in w
Output:-
w =
5 5 5
5 5 5
5 5 5
5 5 5
5 5 5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.