Answer Problem 2 with Matlab Format! ENGR 1181Class Class_17_Application_B C17:
ID: 3599176 • Letter: A
Question
Answer Problem 2 with Matlab Format!
ENGR 1181Class Class_17_Application_B C17: Arrays and For loops You are being assigned three problems. Do all three problems in one script file: Class_17_App.m Include comment statements to help organize your script file. Include the standard header. Problem 1 Create the following vector A=[4 8 12 16 20 24] four different ways. 1. A1 Typing in each element using commas/spaces to separate them 2. A2- Using the [first value : spacing : last value] notation 3. A3 - Using the linspace command 4. A4 - Using a for loop. The for loop must create the vector and add an element to it each time it passes through the loop. Problem 2 Create the following matrix B=123 three different ways. 1. 2. 3. 4. B1 - Typing in each element manually using commas, semicolons etc. to organize them B2 - Creating 3 vectors which are the three rows of B and combining together. B3- Creating 3 vectors which are the three columns of B and combining together Extra Credit: B4 - Using nested for loops (a for loop within a for loop)Explanation / Answer
A1 = [4 8 12 16 20 24];
A2 = [4:4:24];
A3 = linspace(4,24,6);
% using loop to craete a matrix
A4 = [];
for x=4:4:24
A4 = [A4 x];
end
disp(A1);
disp(A2);
disp(A3);
disp(A4);
B = [1 2 3;1 2 3;1 2 3];
B1 = [1 2 3;1 2 3;1 2 3];
B2 = [B(1,:);B(2,:);B(3,:)];
B3 = [B(:,1) B(:,2) B(:,3)];
% nested for loop for a matrix.
B4 = [];
for i=1:3
for j=1:3
B4(i,j) = j;
end
end
disp(B1);
disp(B2);
disp(B3);
disp(B4);
% sample output
% 4 8 12 16 20 24
% 4 8 12 16 20 24
% 4 8 12 16 20 24
% 4 8 12 16 20 24
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
% 1 2 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.