Question

MATLAB: Design a FUNCTION which accepts an array (single row or column) and sorts it into...

MATLAB:

Design a FUNCTION which accepts an array (single row or column) and sorts it into ascending order returning the result.

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

% Matlab Function which accepts an array and sorts it into ascending order returning the result
function y = sorting(x)
n=length(x);
sorted=0; % flag to see sorted or not
k=0;

while ~sorted
sorted=1;
k=k+1;
for j=1:n-k % for each pass exclude last k elements
if x(j) > x(j+1) % checking elements order
temp=x(j);
x(j)=x(j+1);
x(j+1)=temp;
sorted=0; % swap
end
end
end;

y=x;

Add a comment
Know the answer?
Add Answer to:
MATLAB: Design a FUNCTION which accepts an array (single row or column) and sorts it into...
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
  • In matlab, there is a function "size" that accepts an array as a parameter (or input...

    In matlab, there is a function "size" that accepts an array as a parameter (or input argument) and returns the dimensions of that array as another single-dimension array.      For example, if the array were 2 x 2, it would return [2 2]. Assume that input1 is a 1 or 2-dimensional array. Use the size function to obtain the dimensions and save the output into the return argument of this function "output1"     1Do not modify the line below 2-unction...

  • In Matlab A= 3 -5 6 ( 15 7 9 13 5 -4 12 10 2...

    In Matlab A= 3 -5 6 ( 15 7 9 13 5 -4 12 10 2 8 11 4 1 For the matrix A in problem-2, use MATLAB to carry out the following instructions a. Find the maximum and minimum values in each column. b. Find the maximum and minimum values in each row. c. Sort each column in ascending order and store the result in an array P. d. Sort each row in descending order and store the result...

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

  • Create a program called Merge.java that implements a variety of the Merge function (in the Merge...

    Create a program called Merge.java that implements a variety of the Merge function (in the Merge Sort algorithm). The program should be able to do the following: accepts two command line parameters, each specifies a text file containing the integers to be merged. The structure of the files is as follows: For both files, there will be multiple lines, each of which contains one integer. The number of lines is unknown. reads the integers from the text files into two...

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

  • PLEASE USE MATLAB Create a single MatLab script that On a single line (Line 1), defines...

    PLEASE USE MATLAB Create a single MatLab script that On a single line (Line 1), defines matrix A such that A = 1 2 4. 5 7 8 Suppress the echo of A. On a single line (Line 2), use the size function to echo the size of A. On a single line (Line 3), use the length function to echo the length of the entire first row of A. On a single line (Line 4), use linear index notation...

  • This is for Java. Create ONE method/function that will return an array containing the row and...

    This is for Java. Create ONE method/function that will return an array containing the row and column of the largest integer i the 2D array. If the largest number is located on row 2, column 1, the method needs to return row 2 and column one. Do not use more than one method. Use the code below as the main. Please comment any changes. in java Given the main, create a method that RETURNS the largest number found in the...

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

  • use matlab do clearly 2) Write a MATLAB function that takes a structure array holding information...

    use matlab do clearly 2) Write a MATLAB function that takes a structure array holding information about students (name, age, GPA) and returns this array by ordering the students with respect to their age by ascending ordering (smallest to largest). Use bubble sort algorithm for sorting!!!!!

  • Write a C++ program that does the following : 1. Accepts array size from the keyboard....

    Write a C++ program that does the following : 1. Accepts array size from the keyboard. The size must be positive integer that is >= 5 and <= 15 inclusive . 2. Use the size from step 1 in order to create an integer array. Populate the created array with random integer values between 10 and 200. 3. Display the generated array. 4. Write a recursive function that displays the array in reverse order . 5. Write a recursive function...

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
Active Questions
ADVERTISEMENT