PROBLEM 62 Write a MATLAB function vec top11A that returns

PROBLEM 6.2: Write a MATLAB function [vec] = top11(A) that returns a column vector of the eleven “largest” items in the input A. Your function should work properly when A is a scalar, row vector, column vector, and a matrix.

Solution

Matlab Code:

function vec = top11(A)
[r c] = size(A);%gives no. of rows and columns
for i=1:r*c
newA(i) = A(i);%stores in single column
end
sortedA = sort(newA,\'descend\');%sorts the column
vec = sortedA(1:11);%takes first 11 values
end

Output:

>> A = [23 43 67 23 09 23 76 34;29 78 98 30 65 47 23 90]

A =

23 43 67 23 9 23 76 34
29 78 98 30 65 47 23 90

>> vec = top11(A)

vec =

98 90 78 76 67 65 47 43 34 30 29

PROBLEM 6.2: Write a MATLAB function [vec] = top11(A) that returns a column vector of the eleven “largest” items in the input A. Your function should work prope

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site