Matlab code assistance. Please help! % housekeeping clc % clear command window (
ID: 3785419 • Letter: M
Question
Matlab code assistance. Please help!
% housekeeping
clc % clear command window (console)
clearvars % clear workspace
%% Task 1
% create the 3 row vectors shown in Ex 8 using 3 different approaches
% 1. list the values explicitly (square brackets)
% 2. use the colon operator
% 3. use the linspace function
%% Task 2
% create a row vector with a randomly-generated upper bound
% Step 1 generate a random integer on the closed interval [10,25]
myend =
% Step 2: use the colon operator to create vec, a row vector of the integers ranging from
% 1 to myend, incrementing (step value) by 3
vec =
% Step 3: write the assignment statement that transposes vec into a column vector
%% Task 3
% Step 1: Assign v1, a row vector of any 5 values.
v1 =
% Step 2: Assign v2, a row vector of any 6 values.
v2 =
% Step 3:
% For each vector, write the expression that selects only the elements
% in the odd-numbered positions (i.e., the first, third, etc.,value listed in the vector).
% When indexing into each vector, use the "end" keyword and NOT the number of elements.
% Step 4:
% For each vector, write the expression that selects only the elements
% in the even-numbered positions (i.e., the second, fourth, etc.,value listed in the vector).
% When indexing into each vector, use the "end" keyword and NOT the number of elements.
%% Task 4
% Step 1: Create r1, a row vector of the odd values from 1 to 100.
% Do not explicitly list all the values! Use the colon operator and a step value.
% Step 2: Create r2, a row vector of 50 evenly-spaced values ranging
% from -3 to 30, inclusive. Do not explicitly list all the values!
% Step 3: Create mat, the matrix whose first row is r1, and whose
% second row is r2.
% To stack the row vectors, use the semi-colon inside the square brackets
% Step 4: Using two indices, write the expression for the mat value located at row 1, col 3
% Step 5: Assign a new variable c3, a col vector containing all values in mat col 3.
% Step 6: Assign a new variable c39, that contains all values in mat cols 3 through 9 (inclusive)
% Step 7: Assign a new variable c3_9, that contains all values in
% mat columns 3 and 9 only.
%% Task 5
% Step 1: Create three random variables, each of which is a 2x3 matrix
% a) random values should be real numbers on open interval (0,1)
% b) random values should be real numbers on open interval (0,10)
% c) random values should be integers on closed interval [5,20]
% Step 2: Using max, write an expression to find the maximum values in your matrix from Step 1c
% a) largest value in each column (should produce a row vector)
% b) largest value in each row (should produce a col vector)
% c) large value overall (should produce a scalar)
% Step 3: update your matrix from Step 1c.
% The updated version should be a 2x2 matrix, containing the values from cols 1 and 3 only
Explanation / Answer
Example 8 is missing so in task1 i have assumed as my own.
% housekeeping
clc % clear command window (console)
clearvars % clear workspace
% %% Task 1
% % create the 3 row vectors shown in Ex 8 using 3 different approaches
% Approach 1
vector1=[1 2 3 4 5 6 7] % Vector1
% Approach 2
vector2=[1:7]
% Approach 3
vector3=[linspace(1,7,7)]
% %% Task 2
% % create a row vector with a randomly-generated upper bound
% % Step 1 generate a random integer on the closed interval [10,25]
myend = 10+mod((ceil(rand(1,1)*100)),16)
% % Step 2: use the colon operator to create vec, a row vector of the integers ranging from
% % 1 to myend, incrementing (step value) by 3
vec = [1:3:myend]
% % Step 3: write the assignment statement that transposes vec into a column vector
vec_col=vec'
% %% Task 3
% % Step 1: Assign v1, a row vector of any 5 values.
v1 =linspace(1,5,5)
% % Step 2: Assign v2, a row vector of any 6 values.
v2 =linspace(1,11,6)
% % Step 3:
% % For each vector, write the expression that selects only the elements
% % in the odd-numbered positions (i.e., the first, third, etc.,value listed in the vector).
% % When indexing into each vector, use the "end" keyword and NOT the number of elements.
odd_numbered_pos=[v1(1:2:end) v2(1:2:end)]
% % Step 4:
% % For each vector, write the expression that selects only the elements
% % in the even-numbered positions (i.e., the second, fourth, etc.,value listed in the vector).
% % When indexing into each vector, use the "end" keyword and NOT the number of elements.
even_numbered_pos=[v1(2:2:end) v2(2:2:end)]
% %% Task 4
% % Step 1: Create r1, a row vector of the odd values from 1 to 100.
r1=[1:2:100]
% % Do not explicitly list all the values! Use the colon operator and a step value.
% % Step 2: Create r2, a row vector of 50 evenly-spaced values ranging
% % from -3 to 30, inclusive. Do not explicitly list all the values!
r2=linspace(-3,30,50)
% % Step 3: Create mat, the matrix whose first row is r1, and whose
% % second row is r2.
% % To stack the row vectors, use the semi-colon inside the square brackets
r_mat=[r1;r2]
% % Step 4: Using two indices, write the expression for the mat value located at row 1, col 3
value=r_mat(1,3)
% % Step 5: Assign a new variable c3, a col vector containing all values in mat col 3.
c3=r_mat(:,3)
% % Step 6: Assign a new variable c39, that contains all values in mat cols 3 through 9 (inclusive)
c39=r_mat(:,3:9)
% % Step 7: Assign a new variable c3_9, that contains all values in
% % mat columns 3 and 9 only.
c3_9=[ r_mat(:,3) r_mat(:,9)]
% %% Task 5
% % Step 1: Create three random variables, each of which is a 2x3 matrix
% % a) random values should be real numbers on open interval (0,1)
rand_vec1=rand(2,3)
% % b) random values should be real numbers on open interval (0,10)
rand_vec2=rand(2,3)*10
% % c) random values should be integers on closed interval [5,20]
rand_vec3=5+mod((ceil(rand(2,3)*100)),16)
% % Step 2: Using max, write an expression to find the maximum values in your matrix from Step 1c
% % a) largest value in each column (should produce a row vector)
largest_col=max(rand_vec3)
% % b) largest value in each row (should produce a col vector)
largest_row=max(max(rand_vec3',1))'
% % c) large value overall (should produce a scalar)
largest=max(max(max(rand_vec3',1))')
% % Step 3: update your matrix from Step 1c.
% The updated version should be a 2x2 matrix, containing the values from cols 1 and 3 only
updated_mat=[rand_vec3(:,1) rand_vec3(:,3)]