Question

*MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop,...

*MATLAB CODE*

3)Write a function 'mydsort' that sorts a vector in descending order (using a loop, not the built-in sort function).

4)write a function that will receive a vector and will return two index vectors: one for ascending order and one for descending order. Check the function by writing a script that will call the function and then use the index vectors to print the original vector in ascending and descending order.

**Please make sure they work. I have found other examples of these however none of them are working properly. I will rate if the program runs properly.

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

1)

function A=mydsort(A)
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)<A(i+1)
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end
end

Add a comment
Know the answer?
Add Answer to:
*MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a 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
  • Please use MatLab and comment code. Problem 2 Write a MATI AB user-defined function y-M?SotxType): x...

    Please use MatLab and comment code. Problem 2 Write a MATI AB user-defined function y-M?SotxType): x is a vector containing numbers. and Type can be 'Ascending' or Descending'. This function should sort the given vector x in the ascending or descending order based on the user's request and return the desired vector as the output. Do not use MATLAB built-in function sort. Hint: When you call this function you need to use for Ascending or Descending since they are strings.

  • Must be written in C++ Display results and comments Write a program that sorts a vector...

    Must be written in C++ Display results and comments Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions: 1. void selSort (vector <string> & v): sorts the vector using selection sort 2. void display (const vector <string> & v): displays the vector contents 3. int binSearch (const vector <string> & v, string key): searches...

  • in c++ Program 1 Write a program that sorts a vector of names alphabetically using the...

    in c++ Program 1 Write a program that sorts a vector of names alphabetically using the selection sort and then searches the vector for a specific name using binary search. To do that, you need to write three functions I. void selSort (vector string &v: sorts the vector using selection sort 2. void display (const vector <string & v): displays the vector contents . int binSearch (const vector <ing& v, string key): searches the vector for a key returns the...

  • The matlab code for the following problem is appreciated!! QUESTION 3 Write a function fnfr_plot that...

    The matlab code for the following problem is appreciated!! QUESTION 3 Write a function fnfr_plot that will receive two function handles from elementary math functions which accept a vector to first sort in ascending order then calculate. The script will display the results in two separate Figure Windows with the function names in the title. Submit: fnfr_plot.m Browse My Computer Attach File QUESTION 4 Generate 1x100 random vector, x in the range of 0<x<100 by using rand. Input 2 elementary...

  • C programing Write a program to sort numbers in either descending or ascending order. The program...

    C programing Write a program to sort numbers in either descending or ascending order. The program should ask the user to enter positive integer numbers one at a time(hiting the enter key after each one) The last number entered by the user should be -1, to indicate no further numbers will be entered. Store the numbers in an array, and then ask the user how to sort the numbers (either descending or ascending). Call a function void sortnumbers ( int...

  • This is a c++ code /**    Write a function sort that sorts the elements of...

    This is a c++ code /**    Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...

  • 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.

  • b) Prove, using induction, that the following SomeSort function sorts the part of an array from...

    b) Prove, using induction, that the following SomeSort function sorts the part of an array from index first to index last in ascending order. You only need to apply induction to the outer for loop. The swap function used in the code below is the same one from part a. (20 marks) 1 void Some Sort (int arr [], int first, int last) { for (int i = first; i < last; i++) { int minpos = i; for (int...

  • 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...

  • Question 2 (d) Write equivalent code to the one shown below by replacing the while loop...

    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...

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