Question

MATLAB: Write a code using loop for which implements the following algorithm for finding the maximal...

MATLAB: Write a code using loop for which implements the following algorithm for finding the maximal element in a vector (without using the ready Matlab function max):

1) Assume the first element of the vector to be the maximal one and assign its value to MyMax.

2) Take the second number and compare with the current maximum. If the second number is bigger, assign it as the new maximum, otherwise keep the old max as reference.

3) Take the third number and compare with the current maximum. If the third number is bigger, assign it as the new maximum, otherwise keep the old max as reference.

4) Continue until you check all the numbers in the array. Store the resulting maximum in variable MyMax. Work out an analogical one for finding minimum and assign the result to variable MyMin? Test your code using the following vector x = [10 6 14 12 18 2 8 14].

0 0
Add a comment Improve this question Transcribed image text
Answer #1
x = [10 6 14 12 18 2 8 14];
MyMax = x(1);
for i=1:length(x)
    if x(i) > MyMax
        MyMax = x(i);
    end
end
fprintf("Maximum number in vector is %d\n", MyMax)

MyMin = x(1);
for i=1:length(x)
    if x(i) < MyMin
        MyMin = x(i);
    end
end
fprintf("Minimum number in vector is %d\n", MyMin)

Add a comment
Know the answer?
Add Answer to:
MATLAB: Write a code using loop for which implements the following algorithm for finding the maximal...
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
  • please write the code on matlab   using for loop Also practice using for loops to combine...

    please write the code on matlab   using for loop Also practice using for loops to combine two vectors with the same number of elements. do in-between iterations one before the last iteration will have w/index=(last element-1)) = 4x(index=(last_element-1)) - 2(2) last iteration windex=last element)= 4*x[index=last element) - z(1) you need to figure out how to the change in index for vector z within the for loop.

  • matlab Write a script using a loop that will take a vector (of any size) of...

    matlab Write a script using a loop that will take a vector (of any size) of numbers and remove all numbers that are between 5.0 and 9.0 and assign the remaining vector to a nevw variable. Example: A-[1,5,8,10,13,18.20,9,4,6,8] B-[1,10,13,18,20,4]

  • Note: I need matlab code and it must be on first entry, output what the question...

    Note: I need matlab code and it must be on first entry, output what the question needs and i need it without any false in the matlab pls program logic: • Find the index of minimum element with min() function. Make sure to take two outputs while using min(), the second output is the location of the minimum element. • replace the element at minimum index with mean(v) • find sum(v) and assign it to output program: function x =...

  • Write code that implements the following mysterious black box algorithm. -declare a variable with name "x"...

    Write code that implements the following mysterious black box algorithm. -declare a variable with name "x" of type int, and initialize it to 60 -Execute the following statements 13 times: -if x is divisible by 4 and x is divisible by 3, output the number and say that this number "should be divisible by 12" -set x equal to itself minus 2. This MUST be done using shorthand notation, in C++

  • using matlab Create the following matrix B. [18 17 16 15 14 13] 12 11 10...

    using matlab Create the following matrix B. [18 17 16 15 14 13] 12 11 10 9 8 7 6 5 4 3 2 1 Use the matrix B to: (a) Create a six-element column vector named va that contains the elements of the second and fifth columns of B. (6) Create a seven-element column vector named vb that contains elements 3 through 6 of the third row of B and the elements of the second column of B. Create...

  • PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a...

    PLEASE USE MATLAB. The code is basically given to you with the algorithm above. Write a program for solving the system Ar-b by using Gauss-Seidel iterative method discussed in class that uses a computer memory for one iterate only. This could be summarized as follows: Input n -- the size of the system, matrix A-(a(i,j), the vectoir b (b (1), . . . , b(n)), the intitial guess x (x(1), ..., x(n)) (you can use x=b) maximum number of iterations...

  • Write a MATLAB script, which asks the user to input the angle xd (in deg) and...

    Write a MATLAB script, which asks the user to input the angle xd (in deg) and then approximates sine of the angle using the first 5 terms of Taylor series as follows: n+12n-1 sin n-1 (2n-1)! 1 Note that x in Taylor's equation is in radian, so make sure to do the conversion first Start with defining the vector n=1:5 and then use element-wise operation and basic MATLAB commands to calculate the summation. To calculate the factorial of any number,...

  • Write C code to repeatedly ask the user for a number, then once the user enters...

    Write C code to repeatedly ask the user for a number, then once the user enters 0, the code displays the min, max and average of all values entered. To get proper credit, name the variables as listed below and follow the directions carefully. Do not use any break or similar type of statements. To implement the above code, you will need to count the entries - name the counter variable num_count. You will also need to use a variable,...

  • Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts...

    Please write the code using matlab 1) i. Create an anonymous function named optimist, that accepts a single variable as input i Create a row vector Tthat represents the number of seconds in two minutes, starting ii. Call the function optimist on the vector T, store the result in variable R1 and returns the double of that variable from one and ending at a hundred and twenty v. Create an anonymous function named pessimist, that accepts a single variable as...

  • Use the attached Matlab code as a basis to solve the following ordinary differential equation usi...

    Question 1 QUESTION 2 Use the attached Matlab code as a basis to solve the following ordinary differential equation using Euler's method, with timestep of 0.1, from t-0to t-100. d)0) -0 - sin (5vt cos(у Plot y versus t from t=0 to t=100. How many local maxima are on this interval(do not include end points). Be careful to count them all! Answer should be an integer 1 w% Matlab code for the solution of Module 2 3 dt-9.1; %dt is...

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