How to arrange rows in uitable?

조회 수: 1 (최근 30일)
yue ishida
yue ishida 2011년 12월 27일
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
yue ishida
yue ishida 2011년 12월 27일
this is problem in my uitable in GUIDE and no relationship with matlab coder.
Walter Roberson
Walter Roberson 2011년 12월 27일
Well, after you do the ordering work, set() the Data property of the uitable handle to the new data you want to show.

댓글을 달려면 로그인하십시오.

채택된 답변

Aurelien Queffurust
Aurelien Queffurust 2011년 12월 27일
To start :
Answer 1:
[sorted,d]=sort(penalty);
newMatrix= A(d,:);
Answer 2:
parentA= newMatrix(1:2,:);
parentB= newMatrix(3:4,:);
  댓글 수: 4
Andrei Bobrov
Andrei Bobrov 2011년 12월 27일
newMatrix2 = zeros(size(parentA).*[2 1]);
newMatrix2(1:2:end,:) = parentA;
newMatrix2(2:2:end,:) = parentB;
yue ishida
yue ishida 2011년 12월 27일
do you have any idea how to combine parentA and parentB into a matrix again? a newMatrix3 will consist parentA in upper rows and parentB in lower rows.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Andrei Bobrov
Andrei Bobrov 2011년 12월 27일
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
Walter Roberson
Walter Roberson 2011년 12월 28일
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".
yue ishida
yue ishida 2011년 12월 28일
ok, just using else is enough...

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by