How can I permute a 3D magic matrix such that the largest element will be on page 3.

1 view (last 30 days)
A1=[25 16 80 104 90;115 98 4 1 97;42 111 85 2 75;66 72 27 102 48;67 18 119 106 5];
A2=[91 77 71 6 70;52 64 117 69 13;30 118 21 123 23;26 39 92 44 114;116 17 14 73 95];
A3=[47 61 45 76 86;107 43 38 33 94;89 68 63 58 37;32 93 88 83 19;40 50 81 65 79];
A4=[31 53 112 109 10;12 82 34 87 100;103 3 105 8 96;113 57 9 62 74;56 120 55 49 35];
A5=[121 108 7 20 59;29 28 122 125 11;51 15 41 124 84;78 54 99 24 60;36 110 46 22 101];
A=cat(3,A1,A2,A3,A4,A5);
In this example the largest is on page 5
  4 Comments
Geoff Hayes
Geoff Hayes on 25 Nov 2014
Peter - so do you want all combinations of the 5 matrices such that the third will always have A5? So the following would be considered permutations of A
A1 A2 A5 A3 A4
A1 A2 A5 A4 A3
A1 A3 A5 A2 A4
A1 A3 A5 A4 A2
A1 A4 A5 A2 A3
A1 A4 A5 A3 A2
etc.
PETER ADETOKUNBO
PETER ADETOKUNBO on 25 Nov 2014
Rememeber is a 3D array/multidimensional matrix (A=cat(3,A1,A2,A3,A4,A5)). I want its permutation such that the largest element (125) will be on page three, not necessary the whole A5, and still keeping the properties of magic matrix.

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 25 Nov 2014
By "permutation" I assume you mean a permutation among the 3D pages.
[~,ind] = max(A(:));
[~,~,i3] = ind2sub(size(A),ind);
p = 1:size(A,3); p([3,i3]) = p([i3,3]);
A = A(:,:,p);
Note: There are many other permutations of the pages (23 to be exact) that will move the largest element to page 3. This one is a simple swap.
  6 Comments
Image Analyst
Image Analyst on 30 Nov 2014
Can't you just assign the element you want with the max value (125)?
A(1,1,3) = 125;
or do you want to move/translate everything - all elements - with something like circshift()?
PETER ADETOKUNBO
PETER ADETOKUNBO on 30 Nov 2014
Maybe my question is not clear, What I want to do exactly is I want 125 at the corner of slice/page three at any location (A(1,1,3);A(1,5,3),A(5,5,3),A(5,1,3)) and still keep the properties of magic cube. What lines of code do I need to add to what Roger Stafford provided?

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!