Changing matrix column order

조회 수: 45 (최근 30일)
Penny Ibil
Penny Ibil 2020년 5월 18일
댓글: Walter Roberson 2020년 5월 18일
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

채택된 답변

Walter Roberson
Walter Roberson 2020년 5월 18일
reshape(permute(reshape(A,size(A,1),3,[]),[1 3 2]),size(A,1),[])
  댓글 수: 3
Penny Ibil
Penny Ibil 2020년 5월 18일
Can you explain what the [1 3 2] as the dimorder means?
Walter Roberson
Walter Roberson 2020년 5월 18일
permute(X, [1 3 2]) means that dimension 1 should stay the same, and what was dimension 3 should become the new dimension 2, and that what was dimension 2 should become the new third dimension.
permute() is a generalization of transpose(). It moves elements around.
for I = 1 : size(A, 1)
for J = 1 : size(A,2)
for K = 1 : size(A,3);
output(I, K, J) = A(I, J, K);
end
end
end

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

추가 답변 (1개)

Ameer Hamza
Ameer Hamza 2020년 5월 18일
편집: Ameer Hamza 2020년 5월 18일
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
Penny Ibil
Penny Ibil 2020년 5월 18일
I want to say that it has something to do with getting the modulo, but I can't quite see it:
>> mod(1:length(A),3)
ans =
1 2 0 1 2 0 1 2 0
Ameer Hamza
Ameer Hamza 2020년 5월 18일
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?

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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by