How can I arrange or matrix columns, depending on the total sum that has the columns?
5 views (last 30 days)
Show older comments
Hi, I need to arrange a matrix A to look like matrix B.
Matrix A generate new positions at every iteration, so the positions are not fixed.
If there is a column in A (in this case, column 3) that has sum=0, and after it has column that sum=1 (column 4), I want column 4 to move to column 3. Then column 5 move to column 4 and the last column is the one that keeps the sum=0 column.
This is a sequencing problem, so my desire is to have a smooth sequence.
What is the best way to do this?
Thanks!
if true
A = [0 1 0 0 0;
1 0 0 0 1;
0 0 0 1 0;
0 0 0 0 0;
0 0 0 0 0];
B = [0 1 0 0 0;
1 0 0 1 0;
0 0 1 0 0;
0 0 0 0 0;
0 0 0 0 0];
end
0 Comments
Accepted Answer
Roger Stafford
on 24 May 2014
Edited: Roger Stafford
on 24 May 2014
Do
s = sum(A,1);
p = find(s(1:end-1)==0&s(2:end)==1);
B = A(:,[setdiff(1:size(A,2),p),p]);
(Corrected)
2 Comments
Roger Stafford
on 25 May 2014
Patty, in your description you stated "If there is a column in A (in this case, column 3) that has sum=0, and after it has column that sum=1 (column 4), I want ...". However, the code you wrote never checks for a succeeding sum of one. To accomplish what your code does, you can just remove the test for a sum of 1 in the code I gave you:
s = sum(A,1);
p = find(s(1:end-1)==0);
B = A(:,[setdiff(1:size(A,2),p),p]);
It will always agree with the result you obtained.
More Answers (1)
Image Analyst
on 24 May 2014
Patty: Here's an alternate way using sort() that I think does what you want:
% Generate random A.
A = randi(9, [20,6])
% Sum the columns.
sumA = sum(A, 1)
% Sort A in ascending order.
% The sortOrder is what we need here.
[sortedSums, sortOrder] = sort(sumA, 'Ascend')
% Make B sorted in order of increasing column sums of A.
B = A(:, sortOrder)
% Get sums of B to verify it worked
sumB = sum(B, 1)
A =
2 7 1 9 6 7
8 4 2 2 4 7
3 5 2 3 3 7
6 6 2 4 5 1
9 1 3 1 4 7
4 3 3 7 4 5
7 7 2 4 6 2
7 7 3 9 7 1
4 2 9 4 4 8
6 2 7 6 4 2
1 1 6 2 2 2
9 1 2 4 1 6
2 4 2 2 3 9
3 6 1 7 3 5
8 7 9 8 6 7
5 5 7 4 9 2
7 1 6 7 9 9
4 6 3 3 5 5
3 2 2 5 3 7
1 2 6 8 7 1
sumA =
99 79 78 99 95 100
sortedSums =
78 79 95 99 99 100
sortOrder =
3 2 5 1 4 6
B =
1 7 6 2 9 7
2 4 4 8 2 7
2 5 3 3 3 7
2 6 5 6 4 1
3 1 4 9 1 7
3 3 4 4 7 5
2 7 6 7 4 2
3 7 7 7 9 1
9 2 4 4 4 8
7 2 4 6 6 2
6 1 2 1 2 2
2 1 1 9 4 6
2 4 3 2 2 9
1 6 3 3 7 5
9 7 6 8 8 7
7 5 9 5 4 2
6 1 9 7 7 9
3 6 5 4 3 5
2 2 3 3 5 7
6 2 7 1 8 1
sumB =
78 79 95 99 99 100
0 Comments
See Also
Categories
Find more on Numeric Types 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!