Sorting Algorithm for Matlab

it will be a random matrix 3x3.For example=[7 5 1][8 9 2][3 5] One field is empty.I need use sorting algorithm.Looks like=[1 2 3][4 5 6][7 8 ] Empty field will be final.

 采纳的回答

A={[7 5 1],[8 9 2],[3 5]}
idx2=cumsum(cellfun(@numel,A));
idx1=[1 idx2(1:end-1)+1];
C=sort(cell2mat(A));
out=arrayfun(@(ii1,ii2) C(ii1:ii2),idx1,idx2,'un',0);
celldisp(out)

3 个评论

thank you for answer.so other question.numbers move on empty field but do not cross,only forward,back,down up..How can solve this problem?
Please give an example and show the expected result
still 3x3 random matrix for example:
[3 5 8]
[1 _ 4]
[6 7 2] _(empty field) numbers move on empty field but do not cross,only forward,back,down and up..
expected result still same :
[1 2 3]
[4 5 6]
[7 8 _]
it is a kind of game..

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2013-11-21

0 个投票

Sounds to me like you are dealing with the 8 Puzzle; http://en.wikipedia.org/wiki/15_puzzle

类别

帮助中心File Exchange 中查找有关 Shifting and Sorting Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by