Question

In: Computer Science

Matlab Programming Write an M-file to read a real-valued vector of any length from the keyboard...

Matlab Programming

Write an M-file to read a real-valued vector of any length from the keyboard and sort it into descending order, i.e., rearrange all the numbers from the biggest one to the smallest one.

Please test your program with the following 9-element array:

input_vector=[-1, -11, 6,-17, 23, 0, 5, 9, -8]

Solutions

Expert Solution

MATLAB Code:

% MATLAB Script that reads a vector, Sort the vector in Descending order and prints

clc;
clear;

% Reading input vector from user
ipVec = input("Enter a vector: ");

% Sorting vector in descending order using Bubble Sort Logic
% Set size to length of the array passed
size = length(ipVec);
  
% Outer loop runs from 1 to size
for outer_loop=1:size
% Inner loop runs from 1 to size-1
for inner_loop=1:size-1
% Comparing elements
if ipVec(inner_loop) < ipVec(inner_loop + 1)
% Swapping elements
temp = ipVec(inner_loop + 1);
ipVec(inner_loop + 1) = ipVec(inner_loop);
ipVec(inner_loop) = temp;
end %end if statement
end %end for inner_loop
end %end outer_loop

% Printing vector after sorting
fprintf("\nAfter Sorting in Descending Order: ");
disp(ipVec);

____________________________________________________________________________________________

Sample Run:


Related Solutions

1. Given a vector, a, of length n, write down the MATLAB expressions that will correctly...
1. Given a vector, a, of length n, write down the MATLAB expressions that will correctly compute the following: a.        ln (3 + a + a2 ) b.         ea (1 + cos(3a)) c.         cos2 (a) + sin2 (a) d.         tan-1 (a) Test that your solution works for a = 1:0.2:2 2. a. Create a vector of odd whole numbers between 10 and 40.       b. Create a vector of even whole numbers between 25 and 35. 3. Plot...
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio...
show the MATLAB Code with comments and Write an .m file in MATLAB, that records audio (you can record your own voice for 20 seconds), takes Fourier transform of the signal (probably FFT).
Which of the following are subspaces of the vector space of real-valued functions of a real...
Which of the following are subspaces of the vector space of real-valued functions of a real variables? (must select all of the subspaces.) A. The set of even function (f(-x) = f(x) for all numbers x). B. The set of odd functions (f(-x) = -f(x) for all real numbers x). C. The set of functions f such that f(0) = 7 D. The set of functions f such that f(7) = 0
Write a .m function file on MATLAB that reads the parameters stored in the file missile...
Write a .m function file on MATLAB that reads the parameters stored in the file missile data.txt into MATLAB. The function should have the following declaration: function [X0, Y0, Z0, m0, mf, Thmag0, theta, phi, Tburn] = read input( input filename, M id) where input filename is a string variable denoting the name of the file to be read and M_id is an integer which denotes the missile ID. The outputs are the initial position (X0, Y0, Z0), initial and...
Write a C++ program to read characters from the keyboard until a '#' character is read....
Write a C++ program to read characters from the keyboard until a '#' character is read. Then the program will find and print the number of uppercase letters read from the keyboard.
C++ programming question Write a program that will read input from a text file called "theNumbers.txt"...
C++ programming question Write a program that will read input from a text file called "theNumbers.txt" (you will have to provide your own when debugging). The text file that is to be opened is formatted a certain way, namely, there is always one integer, one character (representing an operation), another integer, and then a new line character, with spaces between each item. A sample text file is provided below. theNumbers.txt 144 + 26 3 * 18 88 / 4 22...
For this assignment, using MATLAB you are to read from an Excel file “theInputFile.xlsx” an undetermined...
For this assignment, using MATLAB you are to read from an Excel file “theInputFile.xlsx” an undetermined number of rows and columns. The first task of your program is to find out if the data in the file is in a square matrix. If it is not square the program will give us a statement telling us the data is not complete. If it is square then the program proceeds and will find the average of the rows one row at...
Interpolation Filters. Write a Matlab program to read and upsample the linked voice_samp_8k.wav file (below --...
Interpolation Filters. Write a Matlab program to read and upsample the linked voice_samp_8k.wav file (below -- sampled at Fs=8000 samples per second) by an integer factor L (you can choose, probably in the range from 3 to 7 or so). Then use interpolation filtering to produce the following outputs and listen to them and plot short segments (~50-100 samples) of these outputs: (i) upsample only with no interpolation filtering, (ii) hold interpolation implemented directly as a causal FIR filter, (iii)...
1. Magnitude & Direction of Vector Write a function m-file to find the magnitude and the...
1. Magnitude & Direction of Vector Write a function m-file to find the magnitude and the direction of cosine of vector F which expresses 3 dimension - x, y, and z. Here are requirements for the function m-file. - Parameter-list (input(s)): · 3 dimensional vector · row vector or column vector - undetermined - Return Value (outputs): · Magnitude of a given vector, |F| = √ F2 x + F2 y + F2 z · Angles(degree) from direction of cosine...
Write a C++ program to read in a list of 10 integers from the keyboard. Place...
Write a C++ program to read in a list of 10 integers from the keyboard. Place the even numbers into an array called even, the odd numbers into an array called odd, and the negative numbers into an array called negative. Keep track of the number of values read into each array. Print all three arrays after all the numbers have been read. Print only the valid elements (elements that have been assigned a value). a. Use main( ) as...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT