Question
Matlab problem
Write a function matsort to sort all of the values in a matrix (decide whether the sorted values are stored by row or by column). It will receive one matrix argument and return a sorted matrix. Do this without loops, using the built-in functions sort and reshape. For example: >> mat mat- 4 5 2 1 3 6 7 8 4 9 1 5 >> matsort(mat) 1 4 6 1 4 7 2 5 8 3 5 9 mat [45 2; 1 3 6; 7 8 4; 9 1 5] myMat matsort (mat)
0 0
Add a comment Improve this question Transcribed image text
Answer #1

matsort m:
A user defined function that sorts all elements of matrix M and then returns
sorted matrix.Elements are sorted in ascending order column wise.Save this function in your matlab scripts.

function A=matsort(M)
%MATSORT Sorts all elements of matrix M and then returns
%sorted matrix elements are sorted in ascending order
%cloumn wise
%format of call:matsort(M)
%Returns a matrix
%Get the size of M
[r,c]=size(M)
%Use linear indexing to treat M as a vector built column
%wise sort the vector then reshape back into the
%original size
A=reshape(sort(M(1:numel(M))),r,c)
end
commands entered in matlab window:
make a matrix to test the function
>>mat=[4 5 2;1 3 6;7 8 4;9 1 5]
mat=
4 5 2
1 3 6
7 8 4
9 1 5
In command window call the function by typing the appropriate call format
>>matsort(mat)
1 4 6
1 4 7
2 5 8
3 5 9
Thus the functions arranges the elements in ascending order column wise

Add a comment
Know the answer?
Add Answer to:
Matlab problem Write a function matsort to sort all of the values in a matrix (decide...
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
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