How can I shuffle a matrix

15 visualizzazioni (ultimi 30 giorni)
xia
xia il 14 Gen 2018
Modificato: xia il 17 Ott 2025 alle 6:49
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

Risposta accettata

Stephen23
Stephen23 il 14 Gen 2018
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 Commento
xia
xia il 17 Gen 2018
thank you, this is the answer I am looking for

Accedi per commentare.

Più risposte (1)

Roger Stafford
Roger Stafford il 14 Gen 2018
AB = A;
AB(2,9,6,13,4,11,8,15) = AB(9,2,13,6,11,4,15,8);
  3 Commenti
xia
xia il 14 Gen 2018
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 il 17 Gen 2018
thank you for the answer

Accedi per commentare.

Categorie

Scopri di più su Multidimensional Arrays in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by