Question

In: Computer Science

MATLAB PROBLEM convert the for loop to a while loop. vec= [1 2 3 4 5]...

MATLAB PROBLEM

convert the for loop to a while loop.

vec= [1 2 3 4 5]

newVec= []

for i=vec

if i>5

new vec=[newvec, i]

end

end

end

Solutions

Expert Solution

vec= [1 2 3 4 5]      %vector

newVec= []             % initialize new vector 'newVec'

for i=vec                 % i will contain value of vector 'vec' for each iteration

if i>5                       % check if value of vector 'vec' is greater than 5 (>5) or not.

new vec=[newvec, i] %if yes, then push that value in the new vector 'newVec'.

end                          % end of if condition.

end                         % end of for loop

After Convertion into While Loop

vec= [1 2 3 4 5]     %vector

newVec= []            % initialize new vector 'newVec'
n=size(vec)(2)       % size of vec

i=1                         % initialize the i=1 for iteration
while i<=n             %while loop will run until i<=n

if(vec(i)>5)             % check if value of vector 'vec' is greater than 5 (>5) or not.

newVec=[newVec,vec(i)] % %if yes, then push that value in the new vector 'newVec'.

end                         %end of if condition
i=i+1                      % increment the value of i.

end                        % end of for condition

newVec                 % print the values of new vector

Note: newVec will be empty as no value of vector 'vec' is greater than 5.


Related Solutions

All in C++ programming language 1. a.) convert for loop to while loop example b.) convert...
All in C++ programming language 1. a.) convert for loop to while loop example b.) convert while loop to for loop example 2.) pass one dimension array(and its size) to function example
Study the following code with a while-loop and convert it to a for-loop (fill in the...
Study the following code with a while-loop and convert it to a for-loop (fill in the blanks). int i=4, result=1; while(i>1) { result *= i; i--; } The following for-loop performs the same functionality: int result=1; for (__________ i=4; i _________1;____________) { result *= i; }
Problem 1 ✓ Problem 2 Problem 3 ✓ Problem 4 … Problem 5 … Problem 6...
Problem 1 ✓ Problem 2 Problem 3 ✓ Problem 4 … Problem 5 … Problem 6 ✓ Problem 7 Problem 8 Problem 9 close sidebar webwork / f2019stat213 / stat_213_assignment_3 / 5 STAT 213 Assignment 3: Problem 5 Previous Problem Problem List Next Problem (1 point) To examine the effectiveness of its four annual advertising promotions, a mail order company has sent a questionnaire to each of its customers, asking how many of the previous year's promotions prompted orders that...
I want to convert those codes to assembler code // Problem 1 // for loop J=5...
I want to convert those codes to assembler code // Problem 1 // for loop J=5 for(i=1; i<5; i++) {         j-- } // Problem 2 // if - then - else i=4 if (i < 5) then         j = 3 else         j = 2 // Problem 3 //while loop i = 0 j = 0 while(i==0) {   j++   if j = 5 then         i = j }
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel...
Sentinel While Loop Lab Convert Lab 11 from a counter controlled WHILE loop to a sentinel WHILE loop. Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of...
Mathlab Q1. Instruction Text % For loop code: vec = [45, -1, 7, 0, -37, 4,...
Mathlab Q1. Instruction Text % For loop code: vec = [45, -1, 7, 0, -37, 4, -3]; newvec = zeros(1,numel(vec)); % pre-allocate newvec with zeros for idx = 1:numel(vec) if vec(idx) > 1 & vec(idx) < 0 numerator = 3*vec(idx)^3; denominator = 9*vec(idx)^2 + 3; else numerator = 2*vec(idx)^3 - 2*vec(idx); denominator = 2*vec(idx)^2 - 2/vec(idx); end newvec(idx) = numerator/denominator; end
Please code C# Convert the following for loop into a while loop: for(int count = 8;...
Please code C# Convert the following for loop into a while loop: for(int count = 8; count > 0; count--) { Console.WriteLine(count); }
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting...
Can you rewrite this MATLAB code using a for loop instead of a while loop? %formatting clc, clear, format compact; %define variables k=1; b=-2; x=-1; y=-2; %while loop initialization for k <= 3 disp([num2str(k), ' ',num2str(b),' ',num2str(x),' ',num2str(y),]); y = x^2 -3; if y< b b = y; end x = x+1; k = k+1; end
4. (a) Suppose that τσ=(1 5 2 3)(4) and στ=(1 2 4 5)(3) in S5. If...
4. (a) Suppose that τσ=(1 5 2 3)(4) and στ=(1 2 4 5)(3) in S5. If σ1 = 2, find σ and τ. (b) In Sn, show that σ = τ if and only if σ(τ)^(−1) = ε. ε is the identity permutation. Must be written as a proof. (c) Let σ=(1 2 3) and τ=(1 2) in S3. Show that S3={ε,σ,σ^2,τ,τσ,τ(σ)^2} and that σ^3=ε=τ^2 and στ=τ(σ)^2, then fill out the multiplication table for S3.
```please convert this code to make only using for loop not while loop #include #include #define...
```please convert this code to make only using for loop not while loop #include #include #define MAX_SIZE 500 int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; char part1[100]; char part2[100]; int cursor = 0, i, cnt1 = 0, cnt2 = 0, cnt = 0; int j = 0; int a = 0; int b = 0; int total = 0; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); for (i = 0; i < strlen(tosearch); i++) {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT