Question

Question 2 (d) Write equivalent code to the one shown below by replacing the while loop with a for loop while x <= 15 x=x+1; end [4 marks] (e) Write the small program from the previous question using a single line of code with vector operations. [2 marks] (f) Write a MATLAB script that uses a loop to print the contents of two vectors interleaved in to one vector. Assume both vectors are the same length. For example, if A contains [1, 7, 6, 9], and B contains [4, 4, 2, 5], such that executing the script should display the following: >>zip_script 1 47 46 2 9 5 Hint: Avoid assigning the result in to a vector before printing them [5 marks]

0 0
Add a comment Improve this question Transcribed image text
Answer #1

d)

a = [];
x = 5;
for i=x:15
a(i-4) = i^2;
end

a

=====
Your Code 3 4 5 x=5; for i=x : 15 a(1-4) ?AZ; end CommandLine Arguments Interd Focus Versi Stdin Inputs... O Execute Save My Projects Recent Coll Result... CPU Time: 0.11 sec(s), Memory: 38992 kilobyte(s) el 25 36 49 6481 100 121 144 169 196 225




e) a = [5:1:15].^2

f)



a = [1 7 6 9];
b = [4 4 2 5];
i = 1;
j = 1;
res = ones(1,2*length(a));
index = 1;
flag = true;
while i<=length(a) || j <=length(b)

if(flag)
res(index) = a(i);
i = i + 1;
flag = false;
else
res(index) = b(j);
j = j + 1;
flag = true;
end
index = index + 1;
end

res

=====================================
See Output



Thanks, PLEASE UPVOTE if helpful

Add a comment
Know the answer?
Add Answer to:
Question 2 (d) Write equivalent code to the one shown below by replacing the while loop...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT