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

Matlab 6. Generate a vector with 30 random integer elements with integer values

ID: 3905599 • Letter: M

Question

Matlab

6. Generate a vector with 30 random integer elements with integer values ranging between 20 and 40. Replace all the elements that are not even integers with random integers between 20 and 40. Repeat step b until all the elements in the vector are even integers Your code should keep track of how many times step b was repeated. Display the final vector in the command window as well as a statement that tells how many iterations were required (how many times step b was repeated.) a. b. c. d. e. Sierpioski triangle can be implemented in MATLAB by plotting points iteratively according to one of the following three rules that are selected randomly with equal pproknabiliy Rule 1: xn+,-0.5xn , yn+1-0.5?? Rule 2: xn+10.5xn + 0.25, yn+1-0.5yn + 7. V3 4 Use loops to calculate the x and y vectors, then plot y versus x as individual points (use plot(xx'n'). Start with x1-0 and y1-0. Use a for-loop to run the program 4 times with 10, 100, 1,000 and 10,000 iterations. Use the figure command or subplot to ensure that all 4 plots can be seen after running your code.

Explanation / Answer

Hello Student!

I am happy to help you :

Question 6 solution goes here.

***********Code starts here*************

clc

clear all

a = 20;

b = 40;

% forming the random array

randArr = round((b-a)*rand(30,1)+a);

old = randArr;

% repeat until all the integers become odd

disp('Old vector :');

disp(old);

count = 0;

flag = 1;

while (flag)

check = 0;

for i = 1:30

if mod(randArr(i),2) == 0

randArr(i) = round((b-a)*rand(1,1)+a);

check = 1;

end

end

count = count+1;

if check == 0

flag = 0;

end

end

disp('Iterations required :');

disp(count);

disp('New vector :');

disp(randArr);

***********Code ends here*************

Test Case :

Old vector :
39
37
27
32
26
22
30
37
35
39
27
28
23
27
31
31
23
22
28
37
24
30
23
37
35
31
28
30
38
38

Iterations required :
5

New vector :
39
37
27
33
31
29
33
37
35
39
27
35
23
27
31
31
23
33
29
37
25
21
23
37
35
31
37
27
37
23

Question 7 solution goes here

x1 = 0;

y1 = 0;

rule1x(1) = 0;

rule1y(1) = 0;

rule2x(1) = 0.25;

rule2y(1) = sqrt(3)/4;

rule3x(1) = 0.5;

rule3y(1) = 0;

for i = 2:10000

rule1x(i) = 0.5*rule1x(i-1);

rule1y(i) = 0.5*rule1y(i-1);

rule2x(i) = 0.5*rule2x(i-1)+0.25;

rule2y(i) = 0.5*rule2y(i-1)+sqrt(3)/4;

rule3x(i) = 0.5*rule3x(i-1)+0.5;

rule3y(i) = 0.5*rule3y(i-1);

end

figure ;

plot(rule1x(1:10), rule1y(1:10), rule2x(1:10), rule2y(1:10), rule3x(1:10), rule3y(1:10));

figure ;

plot(rule1x(1:100), rule1y(1:100), rule2x(1:100), rule2y(1:100), rule3x(1:100), rule3y(1:100));

figure ;

plot(rule1x(1:1000), rule1y(1:1000), rule2x(1:1000), rule2y(1:1000), rule3x(1:1000), rule3y(1:1000));

figure ;

plot(rule1x(1:10000), rule1y(1:10000), rule2x(1:10000), rule2y(1:10000), rule3x(1:10000), rule3y(1:10000));

I will upload the screenshot soon (System gets hang when I open matlab, will do it soon). The code is working perfectly!

Thank you. Feel free to ask anything. Please upvote the answer, if you like it.