Write a user-defined function that sorts the elements of a matrix. For the funct
ID: 3645463 • Letter: W
Question
Write a user-defined function that sorts the elements of a matrix. For the function name and arguments use B = matrixsort (A), where A is any size matrix and B is a matrix of the same size with the elements of A rearranged in descending order row after row with the (1,1) element the largest and the (m,n) element the smallest. If available, use the user-defined function down Test your function on a 4 Times 7 matrix with elements (integers) randomly distributed between -30 and 30. Use MATLAB's randi function to generate the initial matrix.Explanation / Answer
function B=matrixsort(A) [d,e]=size(A); c=zeros(1,d*e); k=1; for i=1:d for j=1:e c(k)=A(i,j); k=k+1; end end f=sort(c); B=zeros(d,e); l=1; for i=1:d for j=1:e B(i,j)=f(l); l=l+1; end end ------------------------------------ testing:- a=zeros(4,7); for i=1:4 for j=1:7 a(i,j)=randi([-30,30],1); end end a b=matrixsort(a) ------------------------ output:- a= 25 -23 25 8 -25 -14 3 28 28 -21 29 28 -1 18 -22 -5 25 18 28 10 -28 21 26 11 16 15 -7 9 b= -28 -25 -23 -22 -21 -14 -7 -5 -1 3 8 9 10 11 15 16 18 18 21 25 25 25 26 28 28 28 28 29
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.