How to arrange rows in uitable?
3 views (last 30 days)
Show older comments
I have 2 matrices as below:
A=[1 2 3 0;1 2 4 5;0 9 7 0;1 3 4 6]; A=num2cell(A)
penalty=[-10; -20; -30; 0]
I need to do the code based on algorithm below:
1. arrange matrix A based on penalty. Highest penalty will produce highest row in the matrix. -30>-20>-10>0 Each row in matrix A referred to each row in matrix penalty. e.g: row 1 matrix A referred to -10. Therefore the result will like this:
newMatrix=
0 9 7 0
1 2 4 5
1 2 3 0
1 3 4 6
2. Then, divided the matrix into 2 new matrices, parentA and parentB with equal rows, with the rows above as parentA and rows below as parentB. If the number of newMatrix rows are odd, placed the greater rows in parentA. The result occured as below:
parentA=
0 9 7 0
1 2 4 5
parentB=
1 2 3 0
1 3 4 6
3. Combined back parentA and parentB in a matrix again by rows of parentA as odd rows and parentB as even rows. The result occured as below:
newMatrix2=
0 9 7 0
1 2 3 0
1 2 4 5
1 3 4 6
4. Finally, I need to arrange back the newMatrix2 into original form again as matrix A.
originalMatrixA=
1 2 3 0
1 2 4 5
0 9 7 0
1 3 4 6
This algorithm I need as trick to arrange my code, but I'm not very capable to do it. This code I need to apply to more rows in A such as 100 or 899 rows. I need any help to solve this problem.
3 Comments
Walter Roberson
on 27 Dec 2011
Well, after you do the ordering work, set() the Data property of the uitable handle to the new data you want to show.
Accepted Answer
Aurelien Queffurust
on 27 Dec 2011
To start :
Answer 1:
[sorted,d]=sort(penalty);
newMatrix= A(d,:);
Answer 2:
parentA= newMatrix(1:2,:);
parentB= newMatrix(3:4,:);
4 Comments
Andrei Bobrov
on 27 Dec 2011
newMatrix2 = zeros(size(parentA).*[2 1]);
newMatrix2(1:2:end,:) = parentA;
newMatrix2(2:2:end,:) = parentB;
More Answers (1)
Andrei Bobrov
on 27 Dec 2011
EDIT small added [11:03MSD 28.12.2011]
input matrices
A=[1 2 3 0;1 2 4 5;0 9 7 0;1 3 4 6];
penalty=[-10; -20; -30; 0];
1.
[i1 i1] = sort(penalty);
newMatrix = A(i1,:);
2.
n = size(A,1);
k = ceil(n/2);
parentA= newMatrix(1:k,:)
parentB= newMatrix(k+1:end,:)
3.
newMatrix2 = zeros(size(A));
newMatrix2(1:2:end,:) = parentA;
newMatrix2(2:2:end,:) = parentB;
4.
i2 = reshape([i1;nan(rem(n,2),1)],2,[])';
i2 = i2(:);
i2 = i2(~isnan(i2));
[i3,i3] = sort(i2);
originalMatrixA = newMatrix2(i3,:);
or for 4.
out3 = newMatrix2([1:2:end,2:2:end],:);
[i4,i4] = sort(i1);
originalMatrixA = out3(i4,:);
or
out3 = sortrows([newMatrix2([1:2:end,2:2:end],:), i1],size(A,2)+1);
originalMatrixA = out3(:,1:end-1);
5 Comments
Walter Roberson
on 28 Dec 2011
Under what circumstances can mod(n,2) be neither 0 nor 1 ? If there are no circumstances for that, then there is no point in using "elseif" and you might as well just use "else".
See Also
Categories
Find more on Logical in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!