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

matlab question Write the MATLAB code to create a 1 times 3 vector containing th

ID: 3788101 • Letter: M

Question

matlab question

Write the MATLAB code to create a 1 times 3 vector containing the elements [1, 2, 3]. Additionally, write the MATLAB code to create a 3 times 1 vector with the same elements. Write the MATLAB code to create the vector [2, 4, 6..., 98, 100] and store it in a variable named x. Write the MATLAB code to create a 2 times 2 matrix containing the elements [1, 2, 3, 4] (The order of the numbers do not matter). MATLAB has several types of built-in math functions that can help you perform common operations. Write the MATLAB code that takes the absolute value of -134.

Explanation / Answer

% colon directly creates the row vector 1*3
a = 1:3;
disp('1*3 vector');
disp(a);

% the below notation converts row vector into column vector 3*1
a1 = a(:);
disp('3*1 vector');
disp(a1);

% linspace creates the regularly spaced vector
% starting value ending value and the number of values in that range
x = linspace(2,100,50);
disp('The regularly spaced vector is created as follows');
disp(x);

% directly giving the values of matrix creates a matrix
b = [1 2;3 4];
disp('Matrix');
disp(b);

% abs finds the absolute value in matlab
c=abs(-134);
t = sprintf('Absolute value of -134 is %d',c);
disp(t);


OUTPUT:

1*3 vector
1 2 3

3*1 vector
1
2
3

The regularly spaced vector is created as follows
Columns 1 through 14

2 4 6 8 10 12 14 16 18 20 22 24 26 28

Columns 15 through 28

30 32 34 36 38 40 42 44 46 48 50 52 54 56

Columns 29 through 42

58 60 62 64 66 68 70 72 74 76 78 80 82 84

Columns 43 through 50

86 88 90 92 94 96 98 100

Matrix
1 2
3 4

Absolute value of -134 is 134