sort a matrix without using sort function

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 件のコメント

John
John 2013 年 1 月 22 日
thanks for your help ,I was able to get this script work. thank your very much.
Cedric
Cedric 2013 年 1 月 22 日
+1 for the "homework" tag! If other students were as honest as that, they would get more of my time ;-)

サインインしてコメントする。

回答 (1 件)

Jan
Jan 2013 年 1 月 22 日
編集済み: Jan 2017 年 10 月 1 日

1 投票

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 件のコメント

Pame
Pame 2017 年 10 月 1 日
Old post, but what does v(randperm(n)); actually do?
Stephen23
Stephen23 2017 年 10 月 1 日
編集済み: Stephen23 2017 年 10 月 1 日
The line
v = v(randperm(n));
randomly shuffles the elements of v. You can read the randperm documentation to know what it does.
Pame
Pame 2017 年 10 月 1 日
編集済み: Pame 2017 年 10 月 1 日
Doesen't randperm(n) create an array with random integers from 1 to size(A,2) and with as many elements as the second dimension of matrix A? I don't see how it makes sense to take v(this randperm list).
Btw, is it possible to rewrite this script a bit to make it sort rows in ascending order instead? I know the while any(diff(v) > 0) line makes it not work for ascending numbers, and i haven't found a good while condition for ascending rows.
Jan
Jan 2017 年 10 月 1 日
編集済み: Jan 2017 年 10 月 1 日
@Pame: This code shuffles the elements of each row vector randomly until it is ordered correctly - by accident. This is a hilarious sorting method and I have suggested it, because the OP did not show any own effort. When this was a homework question, we can assume that John has heard something about sorting in the lessons already. Even a short search in the internet will reveal many many examples for (better) sorting algorithms.
I'm not sure if you asking seriously for the opposite sorting direction, or if this is ironical. If you are really interested in this detail, change
any(diff(v) > 0)
to
any(diff(v) < 0)
Pame
Pame 2017 年 10 月 2 日
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 2017 年 10 月 2 日
But "matrices" are "arrays". Do you mean "vectors"?
Do you have a question? If so, I recommend to open a new thread.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeShifting and Sorting Matrices についてさらに検索

タグ

質問済み:

2013 年 1 月 22 日

コメント済み:

Jan
2017 年 10 月 2 日

Community Treasure Hunt

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

Start Hunting!

Translated by