I have matrix nxn, A=[1 2 3 4;5 6 7 8; 9 10 11 12; 13 14 15 16]; I want to shuffle this matrix, which will give AB=[1 2 5 6; 3 4 7 8; 9 10 13 14; 11 12 15 16]. Any help will be highly appreciated

 채택된 답변

Stephen23
Stephen23 2018년 1월 14일

0 개 추천

Here are two methods to rearrange it according to your question. Adjust to suit the size of your matrix.
>> A=[1 2 3 4;5 6 7 8; 9 10 11 12; 13 14 15 16]
A =
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
>> cell2mat(reshape(cellfun(@(m)reshape(m.',2,2).',num2cell(A,2),'uni',0),2,2).')
ans =
1 2 5 6
3 4 7 8
9 10 13 14
11 12 15 16
>> reshape(permute(reshape(permute(reshape(A.',2,2,[]),[1,3,2]),4,2,[]),[1,3,2]),4,[]).'
ans =
1 2 5 6
3 4 7 8
9 10 13 14
11 12 15 16

댓글 수: 1

xia
xia 2018년 1월 17일
thank you, this is the answer I am looking for

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

추가 답변 (1개)

Roger Stafford
Roger Stafford 2018년 1월 14일

0 개 추천

AB = A;
AB(2,9,6,13,4,11,8,15) = AB(9,2,13,6,11,4,15,8);

댓글 수: 3

Jan
Jan 2018년 1월 14일
Insert square brackets for linear indexing, otherwise it accesses a n 8 dimensional array:
AB([2,9,6,13,4,11,8,15]) = AB([9,2,13,6,11,4,15,8]);
xia
xia 2018년 1월 14일
I mean how to change the position of some elements in matrix A to be like AB matrix. Based on some literature using shuffle operators, but only include examples as I have mentioned.
xia
xia 2018년 1월 17일
thank you for the answer

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

카테고리

도움말 센터File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

질문:

xia
2018년 1월 14일

편집:

xia
2025년 10월 17일

Community Treasure Hunt

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

Start Hunting!

Translated by