Q:

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

0

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)

ans =

 1 4 6

 1 4 7

 2 5 8

 3 5 9

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

matsort.m

function outmat = matsort(mat)

% Sorts ALL of the values in a matrix and

% then stores them column-wise

% Format of call: matsort(matrix)

% Returns a matrix, sorted and stored by columns

[r c] = size(mat);

vec = reshape(mat, 1, r*c);

vs = sort(vec);

outmat = reshape(vs,r,c);

end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now