bug in sortrows?

18 views (last 30 days)
Kees Roos
Kees Roos on 29 Mar 2012
[SCd Merging]
part 2: I found a smaller problem where things go wrong. Taking as input n=3 and x = =[0:0.3:1]' the output matrix contains the rows
0.25 0.25 0.5
0.25 0.375 0.375
0.25 0.25 0.5
in this order, obviosly not well ordered!
part 1 I want to generate a matrix whose rows are 'words' of length n=3 over the entries in x = [0:0.2:1]'. Then I remove the zero row, and divide each row by its rowsum. Then all rowsums of the matrix are 1. The resulting matrix has size 215 by 3. The rows of this matrix, denoted as xx, are sorted by using xx = sortrows(xx).
The program that I sent before works fine for other choices of the input x, but for the given choice of x the prograsm fails!
[/merge]
I wrote a small small program that should generated a matrix whose rows are sorted, and whose row sums are 1. With th input n=3, x = [0:0.25:1]' the output contains rows that are not well sorted:
0.111111111111111 0.333333333333333 0.555555555555556;
0.111111111111111 0.444444444444444 0.444444444444444
0.111111111111111 0.444444444444444 0.444444444444444
0.111111111111111 0.444444444444444 0.444444444444444
0.111111111111111 0.333333333333333 0.555555555555556
What can be the reason? Below is my program:
function xx = all_a_2012_03_29(x,n)
%% generate all possible rows
e1 = ones(size(x)); ek = e1;
xx = x;
for k = 2:n;
xx = [kron(x,ek) kron(e1,xx)];
ek = kron(e1,ek);
end
%% remove zero row
i = find(sum(xx') == 0);
xx(i,:) = [];
%% sort each row and scale rowsum to 1
nxx = size(xx,1);
for i = 1:nxx,
xx(i,:) = sort(xx(i,:))/sum(xx(i,:));
end
%% sort the rows
xx = sortrows(xx)
  3 Comments
Matt Tearle
Matt Tearle on 29 Mar 2012
Can you explain what you mean by "fails"? If I do all_a_2012_03_29((0:0.2:1)',3) I get a result and it looks sorted to me.
Thomas
Thomas on 29 Mar 2012
sorts correctly even with the new inputs..

Sign in to comment.

Accepted Answer

Thomas
Thomas on 29 Mar 2012
Sorts well for me:
xx =
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0 1.000000000000000
0 0.200000000000000 0.800000000000000
0 0.200000000000000 0.800000000000000
0 0.200000000000000 0.800000000000000
0 0.200000000000000 0.800000000000000
0 0.200000000000000 0.800000000000000
0 0.200000000000000 0.800000000000000
0 0.250000000000000 0.750000000000000
0 0.250000000000000 0.750000000000000
0 0.250000000000000 0.750000000000000
0 0.250000000000000 0.750000000000000
0 0.250000000000000 0.750000000000000
0 0.250000000000000 0.750000000000000
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.333333333333333 0.666666666666667
0 0.400000000000000 0.600000000000000
0 0.400000000000000 0.600000000000000
0 0.400000000000000 0.600000000000000
0 0.400000000000000 0.600000000000000
0 0.400000000000000 0.600000000000000
0 0.400000000000000 0.600000000000000
0 0.428571428571429 0.571428571428571
0 0.428571428571429 0.571428571428571
0 0.428571428571429 0.571428571428571
0 0.428571428571429 0.571428571428571
0 0.428571428571429 0.571428571428571
0 0.428571428571429 0.571428571428571
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0 0.500000000000000 0.500000000000000
0.111111111111111 0.444444444444444 0.444444444444444
0.111111111111111 0.444444444444444 0.444444444444444
0.111111111111111 0.444444444444444 0.444444444444444
0.125000000000000 0.375000000000000 0.500000000000000
0.125000000000000 0.375000000000000 0.500000000000000
0.125000000000000 0.375000000000000 0.500000000000000
0.125000000000000 0.375000000000000 0.500000000000000
0.125000000000000 0.375000000000000 0.500000000000000
0.125000000000000 0.375000000000000 0.500000000000000
0.142857142857143 0.285714285714286 0.571428571428571
0.142857142857143 0.285714285714286 0.571428571428571
0.142857142857143 0.285714285714286 0.571428571428571
0.142857142857143 0.285714285714286 0.571428571428571
0.142857142857143 0.285714285714286 0.571428571428571
0.142857142857143 0.285714285714286 0.571428571428571
0.142857142857143 0.428571428571429 0.428571428571429
0.142857142857143 0.428571428571429 0.428571428571429
0.142857142857143 0.428571428571429 0.428571428571429
0.166666666666667 0.166666666666667 0.666666666666667
0.166666666666667 0.166666666666667 0.666666666666667
0.166666666666667 0.166666666666667 0.666666666666667
0.166666666666667 0.333333333333333 0.500000000000000
0.166666666666667 0.333333333333333 0.500000000000000
0.166666666666667 0.333333333333333 0.500000000000000
0.166666666666667 0.333333333333333 0.500000000000000
0.166666666666667 0.333333333333333 0.500000000000000
0.166666666666667 0.333333333333333 0.500000000000000
0.200000000000000 0.200000000000000 0.600000000000000
0.200000000000000 0.200000000000000 0.600000000000000
0.200000000000000 0.200000000000000 0.600000000000000
0.200000000000000 0.400000000000000 0.400000000000000
0.200000000000000 0.400000000000000 0.400000000000000
0.200000000000000 0.400000000000000 0.400000000000000
0.200000000000000 0.400000000000000 0.400000000000000
0.200000000000000 0.400000000000000 0.400000000000000
0.200000000000000 0.400000000000000 0.400000000000000
0.222222222222222 0.333333333333333 0.444444444444444
0.222222222222222 0.333333333333333 0.444444444444444
0.222222222222222 0.333333333333333 0.444444444444444
0.222222222222222 0.333333333333333 0.444444444444444
0.222222222222222 0.333333333333333 0.444444444444444
0.222222222222222 0.333333333333333 0.444444444444444
0.250000000000000 0.250000000000000 0.500000000000000
0.250000000000000 0.250000000000000 0.500000000000000
0.250000000000000 0.250000000000000 0.500000000000000
0.250000000000000 0.250000000000000 0.500000000000000
0.250000000000000 0.250000000000000 0.500000000000000
0.250000000000000 0.250000000000000 0.500000000000000
0.250000000000000 0.375000000000000 0.375000000000000
0.250000000000000 0.375000000000000 0.375000000000000
0.250000000000000 0.375000000000000 0.375000000000000
0.272727272727273 0.363636363636364 0.363636363636364
0.272727272727273 0.363636363636364 0.363636363636364
0.272727272727273 0.363636363636364 0.363636363636364
0.285714285714286 0.285714285714286 0.428571428571429
0.285714285714286 0.285714285714286 0.428571428571429
0.285714285714286 0.285714285714286 0.428571428571429
0.300000000000000 0.300000000000000 0.400000000000000
0.300000000000000 0.300000000000000 0.400000000000000
0.300000000000000 0.300000000000000 0.400000000000000
0.333333333333333 0.333333333333333 0.333333333333333
0.333333333333333 0.333333333333333 0.333333333333333
0.333333333333333 0.333333333333333 0.333333333333333
0.333333333333333 0.333333333333333 0.333333333333333

More Answers (1)

Sean de Wolski
Sean de Wolski on 29 Mar 2012
xx looks sorted to me.

Categories

Find more on Shifting and Sorting Matrices in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!