sort a matrix without using sort function

36 visualizaciones (últimos 30 días)
John
John el 22 de En. de 2013
Comentada: Jan el 2 de Oct. de 2017
how can I sort this matrix in descend order(only columns) without using sort function (my homework). for example: A= [
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9 ]
to
B=[
24 17 15 8 1
23 16 14 7 5
22 20 13 6 4
. . . . . .. . .
.. .. . .. . .. . ]
thanks
  3 comentarios
John
John el 22 de En. de 2013
thanks for your help ,I was able to get this script work. thank your very much.
Cedric
Cedric el 22 de En. de 2013
+1 for the "homework" tag! If other students were as honest as that, they would get more of my time ;-)

Iniciar sesión para comentar.

Respuestas (1)

Jan
Jan el 22 de En. de 2013
Editada: Jan el 1 de Oct. de 2017
Here a solution, which I would not submit except with a bold explanation:
B = zeros(size(A));
n = size(A, 2);
for iRow = 1:size(A, 1)
v = A(iRow, :);
while any(diff(v) > 0) % Test if sorted in descending order
v = v(randperm(n, n)); % If not, shuffle the elements randomly
end
B(iRow, :) = v;
end
This is based on the "infinite monkey" theorem: Even a randomly typing monkey will find the solution by accident. In consequence is not guaranteed to finish until your homework is due.
[EDITED] This is not a serious suggestion. If you deliver this as solution of a homework, you might get an extra point for a nerdy kind of humor, perhaps. Ask an internet search engine for better sorting algorithms.
  6 comentarios
Pame
Pame el 2 de Oct. de 2017
I like the insertion sort method, and i've got it to work for arrays, but not for matrixes.
Also currently trying to make a function which sorts matrixes in ascending order so that the lowest value element is in (1,1) and the highest value element is in the last row, last column.
Jan
Jan el 2 de Oct. de 2017
But "matrices" are "arrays". Do you mean "vectors"?
Do you have a question? If so, I recommend to open a new thread.

Iniciar sesión para comentar.

Categorías

Más información sobre Shifting and Sorting Matrices en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by