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

MATLAB 1.) Given a 50 element array called fifty and random numbers between 1 an

ID: 3693503 • Letter: M

Question

MATLAB

1.) Given a 50 element array called fifty and random numbers between 1 and 50, count the occurrences of each number by incrementing the corresponding element (i.e., if a 3 is generated, then element 3 is incremented by 1). You should use a for-loop to generate 235 random numbers. Output the results of the fifty array (you may not use %g):

%for-loop to generate 235 random numbers
num = randi(50); %generates one random number from 1 to 50

%output fifty array

2.) Traverse through two integer arrays – array1 & array2, outputting each elements’ value and whether or not they are equal.  

3.) Determine the average score in a scores array. Then, traverse scores again and determine how many scores are above the average and how many below – output both these number.

4.) An array letters contains elements made up of only single lowercase letters. Determine the number of times vowels occur and the number of times consonants occur.

5.) An array grades contains percentages of test grades.

Populate a corresponding letterGrades array with the letter grade:

grades = [89, 80, 69, 99, 75, . . . , 69, 90 ];

letterGrades = [ ];

90-100: A

80-89 : B

70 - 79: C

60 - 69: D

< 59 : F

6.) You want to the automate grading of the multiple choice part of an exam from the open file test3_Answers.txt, using the file handle inFile. Populate an array called test3Answers that contains the correct answers to the test questions. You may assume one answer on each line of the input file.

Next, determine the number of questions student1 has gotten correct, and whether or not he/she passed the exam. A passing grade is any score 21 or greater (60%).

student1 = {‘A’, ‘B’, ‘D’, ‘D’, ‘D’, ‘C’, ‘C’, ‘A’, ‘B’, ‘B’, ‘C’, ‘A’, . . ., ‘A’, ‘A’, ‘D’};

index = 1; %index used to populate test3Answers

test3Answers = [ ];
correct = 0;

while ~feof(inFile)
>

%populate test3Answers array

90-100: A

80-89 : B

70 - 79: C

60 - 69: D

< 59 : F

Explanation / Answer

program : >> u = rand(3) %Generates a 3x3 matrix
u =
0.8147 0.9134 0.2785
0.9058 0.6324 0.5469
0.1270 0.0975 0.9575
>> u = rand(1,5) %Generates a 1x5 matrix
u =
0.9649 0.1576 0.9706 0.9572 0.4854
>> u = a + (a-b)*rand(m,n,p,...)
>> g = randn(2,3) %Generates a 2x3 matrix
g =
0.7147 -0.1241 1.4090
-0.2050 1.4897 1.4172
>> M = ones(1,4);
>> g = randn(size(M))   %Generates a matrix of the same size as M with normally distributed values
g =
0.6715 -1.2075 0.7172 1.6302
>> m = 10;   %Mean
>> v = 9;   %Variance
>> d = 1000; %Length
>> g = m + sqrt(v)*randn(d,1);  
>> mean(g) %Mean
ans =
9.8529
>> var(g)   %Variance
ans =
8.6272
>> std(g)   %Standard Deviation
ans =
2.9372
>> s = 3; %Standard deviation
>> g = m + s*randn(d,1);  
>> var(g)   %Variance
ans =
9.0355
>> std(g)   %Standard Deviation
ans =
3.0059
>> b = randi(x, m, n);
>> b = randi(10, 2, 4)
b =
9 4 4 10
2 5 8 2
>> b = randi([x y], m, n)
>> v = 0.1:0.1:1;
>> randv = v(randi(length(v)))
randv =
0.9000
>> A = rand(10,5,2);
>> sA = size(A);
>> randA = A(randi(sA(1)),randi(sA(2)),randi(sA(3)))
randA =
0.6520
>> b = randperm(10)
b =
1 9 10 5 7 2 4 3 6 8
>> b = randperm(10,3)
b =
7 4 8
>> c = randi(10,1,3)
c =
7 2 2
>> deck = {'One','Two','Three','Four'};
>> shuffleddeck = deck(randperm(length(deck)))
shuffleddeck =
'Three' 'Two' 'Four' 'One'
>> hand = deck(randperm(length(deck),2))
hand =
'Two' 'Four'