Question
Matlab problem

88 2. A vector, x, containing values between 0 and 5 is generated randomly below. 8 The length of x is also determined at random. 8 a) set up a for loop to run over all elements of x use the length command b) inside the for loop, use a switch construct to find the odd values s and the even values (including zero) use two case commands. c) inside the case for the odd values use an if so that if a 3 or a 5 is found, that value is replaced by 8 its square leave any 1s unchanged d) inside the case for the even values, use an if so that if a 4 is found, that value is replaced by its square root leave any os or 2s unchanged. 8 So, 1s, 0s, and 2 s will stay the same 3 s and 5s will be replaced s by their square 4s will be replaced by their square root Example output for x before (top row) and after (bottom row) running through the for switch and if constructs 25 printf (InIn#2 n clear all xaround (5*rand (1,5 round (5*rand)));disp (x); your code goes here s Given disp (x)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is the code for you:

fprintf('\n\n#2\n');

clear all

%A vector x, containing valuews between 0 and 5 is generated randomly.

%The length of x is also dtermined at random.

x = round(5*rand(1, 5+round(5*rand)));

disp(x);

%Set up a for loop to run over all elements of x.

for i = 1 : length(x)

%Inside the for loop use a switch construct to find the odd values:

switch(mod(x(i), 2))

case 1

%Inside the case for the odd values, use an if so that if a 3,

%or 5 is found, that value is replaced by its square.

if x(i) == 3 || x(i) == 5

x(i) = x(i) * x(i);

end

case 0

%Inside the case for even values, use an if so that a 4 is

%found, that value is replaced by its square root.

if x(i) == 4

x(i) = sqrt(4);

end

end

end

disp(x);

And the output screenshot is:

MATLAB Window Help ? @ 1.21 GB?0 < >???4) 100% E ? Thu 9 Mar 03:19 MATLAB R2013a ANANDA KUMAR THUMMAPUDI d Editor - Users/AnandakO PUBLISH HOME Search Documentation ? Ilal Find Files Insert Ed fr Find Files Compare Import Save New Variable Open Variable te compare ? Comment 9eas: g sa FindScript Go To ? New Open Save Indent CODE SIMULINK ENVIRONMENT RESOURCES New New Open Data Workspace Clear Workspace EDIT NAVIGATE VARIABLE VectorOrMatrix.m DrawConcentricSquares.m 1- fprintf(\n\n#2 \n); 2 -clear all ÷ ÷ B? -/> Users?AnandaKumarThummapudi?Documents MATLAB Current F. Command Window Works... O A vector x, containing valuews between 0 and 5 is g eThe length of x is also dtermined at random. Name New to MATLAB? Watch this Video, see Examples, or read G 3Dcurves 5 -round (5*rand (1, 5+round (5*rnd 6 -disp(x) RandomValuesofEvenAndodds Wizards 3DParam . . . A6.dat A7.dat Abs_erro Address... 8Set up a for loop to run over all elements of x 8-for i1 length(x) %Inside the for loop use a switch construct to f switch (mod(x(i), 2)) case 1 %Inside the case for the odd values, use Bor 5 is found, that value is replaced bAlgebrai... if x(i)3 x()5 RandonValuesofEvenAndOdda amortiza. A) analyzeT en Animate.m case0 Inside the case for even values, use anApartme... %found, that value is replaced by its if x(i) =-4 Arbitrary... AreaOfFi Arithmet... Array2D. Array3D. x()sqrt (4) RandomValuesofEvenAndOdds Comm... 23 nthFi nthFi nthFi nthFi nthF? clc 25 disp (x) ArrayGen.. ?ArrayMa. RandonValueBofEvenAndodda askacres. AssignV... Automo.. Average. Average. . . clc Rando: 25 B.png bank.m bankDe Rando: clc AreaOfFigurem

Add a comment
Know the answer?
Add Answer to:
Matlab problem A vector, x, containing values between 0 and 5 is generated randomly below. %...
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
  • Problem 3 (13 pts): (modified from Attaway Problem 3.39) a) (5 pts) Using the Matlab rand () func...

    Problem 3 (13 pts): (modified from Attaway Problem 3.39) a) (5 pts) Using the Matlab rand () function, create a script that generates a row vector (1 row X 20 columns) of random numbers, each with magnitude between 0 and 5 b) (3 pts) Use the round() function to turn all of the values into whole numbers by rounding down (truncate the decimal part on your vector) c) (5 pts) Add code to your script to generate a new vector...

  • matlap Question 1 1. Consider the following Matlab program 5 - (2 < 3) & (C2...

    matlap Question 1 1. Consider the following Matlab program 5 - (2 < 3) & (C2 > 3) 1 (1 - 0)) What is the final value of s? A. True B. *1 CO D. false 2- Which expression tests whether variable x is between (but not the same as) the values 5 and 10 (a) 5 <x< 10 (b) 5 <= x <= 10 (c) 5 < X & X > 10 (d) x < 10 & 5 <...

  • Solve in MATLAB Problem 3: Given the vector x- [2 1 0 6 2 3 5...

    Solve in MATLAB Problem 3: Given the vector x- [2 1 0 6 2 3 5 3 4 1 3 2 10 12 4 2 7 9 2 4 51 use a for loop to (a) Add up the values of all elements in x (b) Compute the cumulative sum, y, of elements in x You can check your results using the built-in functions sum and cumsum. Q.5 What is the value of the sum of elements in vector x?...

  • MATLAB Question: TASK 5 12 MARKS -L06N] The rounded-square-root (RSR) of a positive integer n is...

    MATLAB Question: TASK 5 12 MARKS -L06N] The rounded-square-root (RSR) of a positive integer n is defined as the square root of n rounded to the nearest integer. Adapting Heron's method to integer arithmetic allows the rounded-square-root of n to be calculated as follows. Let d be the number of digits of number n d-1 If d is odd, then Xo = 2 × 107- If d is even, then Xo-7 × 107- where xo is the starting guess for...

  • I need this program in MATLAB with different or another code. INPUT: x = [0 1...

    I need this program in MATLAB with different or another code. INPUT: x = [0 1 8 12 27]; y = [1 2 3 4 5]; nx = length(x); ny = length(y); n = length(x); if nx ~= ny display('Error. La cantidad de datos en x no es igual que los datos en y') end Sx = sum(x); Sxx = sum(x.^2); Sy = sum(y); Sxy = sum(x.*y); a1 = (n*Sxy - Sx*Sy)/(n*Sxx-(Sx)^2); a0 = (Sxx*Sy - Sxy*Sx)/(n*Sxx - (Sx)^2); m...

  • SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5...

    SHELL SCRIPT: Provide the following questions with the codes and command line Problem 5: Take 5 input arguments in a for loop, but only add the even i values to sum and display the result. Problem 6: Write a shell script that accepts path of the directory and returns a count of all the files within the specified directory. (Hint: use alias to perform cd operation) Problem 7: Write a shell script that takes an ‘count’ value as an input...

  • S.A vector IS givell by [5, 17,-3, 8, 0,-7, 12, 15, 20,-6, 6, 4,-7, 16]. Write...

    S.A vector IS givell by [5, 17,-3, 8, 0,-7, 12, 15, 20,-6, 6, 4,-7, 16]. Write a program as a 3 or 5, and, raises to the power of 3 the elements that are script tile that doubles the elements that are positive and are divisible by negative but greater than -5. following values. The value of each element in the first row is the number of the 6. Write a program in a script file that creates an matrix...

  • Newton's Method in MATLAB During this module, we are going to use Newton's method to compute...

    Newton's Method in MATLAB During this module, we are going to use Newton's method to compute the root(s) of the function f(x) = x° + 3x² – 2x – 4 Since we need an initial approximation ('guess') of each root to use in Newton's method, let's plot the function f(x) to see many roots there are, and approximately where they lie. Exercise 1 Use MATLAB to create a plot of the function f(x) that clearly shows the locations of its...

  • 2. (a) We want to find the root x of the function f(x); that is, we need f(r) = 0 . This can be done using Newton&#3...

    2. (a) We want to find the root x of the function f(x); that is, we need f(r) = 0 . This can be done using Newton's method, making use of the iterative formula f(xn) Show that the sequence ofiterates (%) converges quadratically if f'(x) 0 in some appropriate interval of x-values near the root χ 9 point b) We can get Newton's method to find the k-th root of some number a by making it solve the non-linear cquation...

  • 1. (a) We need to calculate accurate values of the function for very large values of x. However, it is found that ju...

    1. (a) We need to calculate accurate values of the function for very large values of x. However, it is found that just programming this formula into a computer gives very poor accuracy for large x Explain why this happens, and show how to re-write the function so that it can be used reliably, even when x is large. [6 points] (b) In diffraction theory, it is sometimes necessary to evaluate the function sin θ f(x) for small to moderate...

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