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.

1 Comment

what sorting algo are you looking for?

Sign in to comment.

 Accepted Answer

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 Comments

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..

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 21 Nov 2013
Sounds to me like you are dealing with the 8 Puzzle; http://en.wikipedia.org/wiki/15_puzzle

Categories

Community Treasure Hunt

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

Start Hunting!