Matrix rows and columns swapping

조회 수: 122 (최근 30일)
Rakesh Belchandan
Rakesh Belchandan 2019년 11월 11일
댓글: Walter Roberson 2022년 2월 24일
I want to completely change rows and column simultaneously (please see the picture attached)
Is there a function which can do this?
Thanks,
Rakesh
  댓글 수: 2
Logan Hall
Logan Hall 2021년 2월 24일
For anyone who is looking to swap an array:
Array = Array.';
btw sorry for reviving a dead thread.
Walter Roberson
Walter Roberson 2021년 2월 24일
That does not solve the user's problem at all. The user needs the upper left corner (a 1 in their data) to go to the middle, but using .' would leave the upper left corner exactly where it is.

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

답변 (3개)

Walter Roberson
Walter Roberson 2019년 11월 11일
There is no function that can do that simultaneously, at least not the first time.
For any given row and column exchange pattern, it is possible to pre-process the pattern so that doing the same exchange for multiple different arrays would take place simultaneously for that one matrix. Something like
temp = preprocess_exchange(rows_to_exchange, columns_to_exchange);
newA = A(temp);
newB = B(temp);
where preprocess_exchange is a function that would need to be written.
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 11월 12일
I should correct myself: you can do this with indexing.
>> A = [1 2 3;4 5 6;7 8 9]
A =
1 2 3
4 5 6
7 8 9
>> A([2 1 3],[2 1 3])
ans =
5 4 6
2 1 3
8 7 9
Rakesh Belchandan
Rakesh Belchandan 2019년 11월 12일
Thanks. That works for me.

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


Akira Agata
Akira Agata 2019년 11월 12일
I don't think rows and column can be randomly changed simultaneously.
There should be at least 2 steps, like this:
% Input 3-by-3 matrix
A = reshape(1:9,3,3)';
% Randomize rows and columns
A = A(randperm(3),:);
A = A(:,randperm(3));

Franco Garcia
Franco Garcia 2022년 2월 24일
편집: Franco Garcia 2022년 2월 24일
You can actually do that easily with just proper indexing. According to the example in the attachment:
A=[1,2,3;4,5,6;7,8,9] % The input in your example
A = 3×3
1 2 3 4 5 6 7 8 9
B=A([2,1,3],[2,1,3]) % Your expected output
B = 3×3
5 4 6 2 1 3 8 7 9
Regards!

카테고리

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

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by