Hello: I am providing two matlab codes, and I would like someone to go through t
ID: 3795868 • Letter: H
Question
Hello:
I am providing two matlab codes, and I would like someone to go through the steps and show me how to find the requiremnts.
Q1: What is the value of a2?
v=1:8;
Trial>> a2=[];
Trial>> for i=1:length(v)
if mod(v(i),3)==0
a2=[a2,v(i)];
end
end
THE ANSWER IS a2=[ 3,6] ....HOW DID THEY GET THIS ANSWER????
Q2: what are the values of a4 and b4?
a4=0;
b4=0;
while a4 <=10
b4=b4+1;
a4=a4+2;
end...
The answer is a4 =12 and b4 =6... but how?? SHOW ME PLEASE STEP BY STEP
Explanation / Answer
Q1,) They define a2 as an empty vector. Now, i is iterating from 1 to 8 (i.e. length of v).If element of v at index i is a multiple of 3 then it is stored in a2. That's why, it contains 3 and 6.
Q2.) Initially, a4 =0 and b4 = 0;
Now, since, a4 is less than and equal to 10, we will enter into while loop. Now, b4 =0+1=1, and a4 = 0+2 =2.
Since, a4 is less than and equal to 10, we will enter into while loop. Now, b4 = 1+1=2, and a4 = 2+2 = 4.
Since, a4 is less than and equal to 10, we will enter into while loop. Now, b4 = 2+1=3, and a4 = 4+2 = 6.
Since, a4 is less than and equal to 10, we will enter into while loop. Now, b4 = 3+1=4, and a4 = 6+2 = 8.
Since, a4 is less than and equal to 10, we will enter into while loop. Now, b4 = 4+1=5, and a4 = 6+2 = 10.
Since, a4 is equal to 10, we will enter into while loop. Now, b4 = 5+1=6, and a4 = 10+2 = 12.
Since, a4 is greater than 4, therefore it will come out of the loop and we have a4=12 and b4=6.
Hope it helps, do give your response.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.