How to arrange rows in uitable?

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 Kommentare

Jan
Jan am 27 Dez. 2011
I do not see the connection to UITABLE and the tag "Matlab Coder". Do you use the term "uitable", but mean a matrix?
yue ishida
yue ishida am 27 Dez. 2011
this is problem in my uitable in GUIDE and no relationship with matlab coder.
Walter Roberson
Walter Roberson am 27 Dez. 2011
Well, after you do the ordering work, set() the Data property of the uitable handle to the new data you want to show.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Aurelien Queffurust
Aurelien Queffurust am 27 Dez. 2011

1 Stimme

To start :
Answer 1:
[sorted,d]=sort(penalty);
newMatrix= A(d,:);
Answer 2:
parentA= newMatrix(1:2,:);
parentB= newMatrix(3:4,:);

4 Kommentare

yue ishida
yue ishida am 27 Dez. 2011
How to code to many rows in A more? such as 30 rows or 120 rows.
Andrei Bobrov
Andrei Bobrov am 27 Dez. 2011
n = size(A,1);
parentA= newMatrix(1:n/2,:);
parentB= newMatrix(n/2+1:end,:);
Andrei Bobrov
Andrei Bobrov am 27 Dez. 2011
newMatrix2 = zeros(size(parentA).*[2 1]);
newMatrix2(1:2:end,:) = parentA;
newMatrix2(2:2:end,:) = parentB;
yue ishida
yue ishida am 27 Dez. 2011
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Andrei Bobrov
Andrei Bobrov am 27 Dez. 2011

0 Stimmen

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 Kommentare

yue ishida
yue ishida am 27 Dez. 2011
i got an error when try to execute this code.
??? Subscripted assignment dimension mismatch.
how to manage it?
yue ishida
yue ishida am 28 Dez. 2011
actually i got error when I have tested it with odd rows, beside than using matrix A, which is even rows, it still worked. How can I manage my problem?
yue ishida
yue ishida am 28 Dez. 2011
2. by using selection if
check=mod(n,2);
iseven=0;
if check==iseven
parentA= newMatrix(1:n/2,:);
parentB= newMatrix(n/2:end-1,:);
elseif check~=iseven
parentA= newMatrix(1:n/2+1,:);
parentB= newMatrix(n/2:end-1,:);
end
Walter Roberson
Walter Roberson am 28 Dez. 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".
yue ishida
yue ishida am 28 Dez. 2011
ok, just using else is enough...

Melden Sie sich an, um zu kommentieren.

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by