how can i sort a matrix (all rows and columns are sorted) without using any special function like "sort"?

2 ビュー (過去 30 日間)
i guess it has to do something with "min" and "max" functions...
for example:
A = [2 3 4 5; 6 9 1 5]; %%%"A" can be of any size %%%
%%%B = sorted A %%%
min(A) = [2 3 1 5]; %%%1st unsorted row of B %%%
max(A) = [6 9 4 5]; %%%2nd unsorted row of B %%%
now i've to sort the rows...
any idea?

回答 (3 件)

Jan
Jan 2012 年 12 月 6 日
This is a perfect question for an internet research:
or

Babak
Babak 2012 年 12 月 6 日
編集済み: Babak 2012 年 12 月 6 日
If you don't want to use sort() then you can write your own sorting algorithm. Like this one (untested):
function B = mysortfunc(A)
if size(A,1)*size(A,2)~=length(A)
errordlg('enter a vector');
return
end
if size(A,2)~=1
A=A';
end
rem_A = A;
for j=1:length(A)
[value,index] = min(rem_A);
B(j) = value;
if index>1
A1 = rem_A(1:index-1);
else
A1=[];
end
if index<length(rem_A)
A2 = rem_A(index+1:end);
else
A2 = [];
end
rem_A = [A1 , A2]
end
  3 件のコメント
José-Luis
José-Luis 2012 年 12 月 7 日
1000 years? Babbage would be impressed...
Jan
Jan 2012 年 12 月 9 日
I'm convinced, that even the scroll of parchment in the Egypt libraries have been sorted with smarter methods 5000 years ago, but I cannot find any resources to prove this.
The optimal sorting machine is still the SFL (spaghetti fork lifter): Cut spaghetti noodles according to the values to be sorted. Lift them up and push them against a wall. The processing time does not depend on the number of elements and even the pre-processing is only O(n).

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


Pritesh Shah
Pritesh Shah 2012 年 12 月 7 日
Simple Solution A = [2 3 4 5; 6 9 1 5]
A =
2 3 4 5
6 9 1 5
>> sort(min(A))
ans =
1 2 3 5
  1 件のコメント
John Petersen
John Petersen 2012 年 12 月 7 日
You used 'sort' which he specifically requested not to be used, and he also specified that A could be any size.

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

カテゴリ

Help Center および File ExchangeOperators and Elementary Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by