Changing matrix column order
Show older comments
An having difficulty expressing the result I would like, but it is shown in the example below. How can I change the order of matrix A's columns so that they are like matrix B, regardless of the data values.
A =
1 2 3 11 22 33 111 222 333
1 2 3 11 22 33 111 222 333
1 2 3 11 22 33 111 222 333
1 2 3 11 22 33 111 222 333
B =
1 11 111 2 22 222 3 33 333
1 11 111 2 22 222 3 33 333
1 11 111 2 22 222 3 33 333
Accepted Answer
More Answers (1)
Ameer Hamza
on 18 May 2020
Edited: Ameer Hamza
on 18 May 2020
For this particular example
A = ...
[1 2 3 11 22 33 111 222 333
1 2 3 11 22 33 111 222 333
1 2 3 11 22 33 111 222 333
1 2 3 11 22 33 111 222 333];
idx = [1 4 7 2 5 8 3 6 9];
B = A(:, idx);
Result
>> B
B =
1 11 111 2 22 222 3 33 333
1 11 111 2 22 222 3 33 333
1 11 111 2 22 222 3 33 333
1 11 111 2 22 222 3 33 333
Is there a general rule that can be expressed mathematically?
2 Comments
Penny Ibil
on 18 May 2020
Ameer Hamza
on 18 May 2020
Do you want to move all columns start with 1 to the left in increasing order, then columns of 2, then 3, so on. Something like this?
Categories
Find more on Creating and Concatenating Matrices 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!